Announcement

Collapse
No announcement yet.

C help: how do I use a string to specify a filename?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C help: how do I use a string to specify a filename?

    I'm going to be outputting data to a bunch of different files with slightly different names.

    The output filename should be of the form

    /home/mmcevoy/3year/princcomp/ver2/tempnumber.dat

    where number is an integer I'm pulling out of a list (and temp stays as a literal part of filename)

    so, first I define the C-string

    char dat[] = ".dat\"";

    then I define my list of numbers as list

    I set i to a specific integer and test by assigning

    char tfilename[200];
    sprintf(tfilename, "\"/home/mmcevoy/3year/princcomp/ver2/temp%d%s", list[i], dat);

    when I do

    printf("%s", tfilename);

    the output reads

    "/home/mmcevoy/3year/princcomp/ver2/temp1238.dat" (1238 is the right number)

    but when I do

    FILE *filet;
    filet = fopen(tfilename, "w");
    fclose(filet);

    it compiles, but I get a segfault when I attempt to run

    (using gcc compiler)
    12-17-10 Mohamed Bouazizi NEVER FORGET
    Stadtluft Macht Frei
    Killing it is the new killing it
    Ultima Ratio Regum

  • #2
    Check whether you have write permission on the destination folder.

    Comment


    • #3
      Actually, I think you have extra quotation marks in the string (they shouldn't be there).

      Comment


      • #4
        a) I do have write permission to that folder

        b) If I don't need quote marks then why do I need quote marks when I manually specify the filename?

        I can't test right now since I'm back home. Actually, I could, but I don't feel like sshing in to my account.
        12-17-10 Mohamed Bouazizi NEVER FORGET
        Stadtluft Macht Frei
        Killing it is the new killing it
        Ultima Ratio Regum

        Comment


        • #5
          Does

          printf("%s", tfilename);

          output

          "/home/mmcevoy/3year/princcomp/ver2/temp1238.dat"

          or

          /home/mmcevoy/3year/princcomp/ver2/temp1238.dat

          If it's the former, fopen() will throw an error since it expects the filename without the quotation marks.

          When you

          fopen( "somefile.dat", "w" );

          you actually pass the filename without the quotation marks, they simply delineate the string from the rest of the code.

          Comment


          • #6
            Anyway, I'm not 100% sure since I haven't written any C code in about five years, but it doesn't cost you anything to try

            Comment


            • #7
              it prints the first one

              what I don't get is that if I wanted to manually type in the filename I would have to write

              FILE *filet;
              filet = fopen("/home/mmcevoy/3year/princcomp/ver2/temp1238.dat", "w");
              fclose(filet);

              but when I input the filename as a string the string does not need quote marks

              I'm not the greatest programmer in the world, so if there's an obvious reason that's probably what I'm not getting
              12-17-10 Mohamed Bouazizi NEVER FORGET
              Stadtluft Macht Frei
              Killing it is the new killing it
              Ultima Ratio Regum

              Comment


              • #8
                Because the quote marks in the manually entered filename are what make it a string. The actually string passed to the function doesn't include quote marks.

                Comment


                • #9
                  are the double quote marks somehow the method that the fopen function uses to parse the typing into a string, therefore if it's already a string it doesn't need them?

                  EDIT: I guess so. Thanks, guys
                  12-17-10 Mohamed Bouazizi NEVER FORGET
                  Stadtluft Macht Frei
                  Killing it is the new killing it
                  Ultima Ratio Regum

                  Comment


                  • #10
                    Re: C help: how do I use a string to specify a filename?

                    try // instead of /

                    also, this is unix so you probably need to do something like ./filename

                    Comment


                    • #11
                      a) / is not a reserved symbol; \ is

                      b) I'm pretty sure vetlegion and kuci already explained the answer
                      12-17-10 Mohamed Bouazizi NEVER FORGET
                      Stadtluft Macht Frei
                      Killing it is the new killing it
                      Ultima Ratio Regum

                      Comment


                      • #12
                        Re: Re: C help: how do I use a string to specify a filename?

                        Originally posted by Whoha
                        also, this is unix so you probably need to do something like ./filename
                        No, I don't

                        I'm not asking it to force to current directory; I'm asking it to write to the same directory no matter where I run program from in my home
                        12-17-10 Mohamed Bouazizi NEVER FORGET
                        Stadtluft Macht Frei
                        Killing it is the new killing it
                        Ultima Ratio Regum

                        Comment


                        • #13
                          you've got it all right, disregard what I said.

                          Comment


                          • #14
                            Thanks, though

                            12-17-10 Mohamed Bouazizi NEVER FORGET
                            Stadtluft Macht Frei
                            Killing it is the new killing it
                            Ultima Ratio Regum

                            Comment


                            • #15
                              Why are you using sprintf? I've always been able to just initialize such strings.

                              Comment

                              Working...
                              X