Thread: trouble using string to open file

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    trouble using string to open file

    I'm having trouble figuring out exactly why my program is telling me it can't find files, but i've narrowed it down.

    Here's a snippet of the code:

    Code:
    char            *filename;
    char             num[10];
    FILE              *imageFileP;
    
    sprintf(num, "%d", i);  // i is an integer that represents file name before the dot
    strcpy(filename, num);
    strcat(filename, ".");
    strcat(filename, app->FileExt); // FileExt is a char size [10]
            
    printf("filename is: '%s'\n", filename); 
             
    imageFileP = fopen(filename,"r"); 
                     
    printf("filename is: '%s'\n", filename);
    on the first print, filename looks exactly right, but on the second print the filename is empty. I believe something is wrong with filename -- even if it looks right -- because I am using char buffers of fixed size to build it. Can someone show me how to adjust my code to make this work?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How is filename declared? It needs to have enough space to store the entire filename.

    Since you're already using sprintf(), consider using something like this.
    Code:
    sprintf(filename, "%d.%s", i, app->FileExt);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    filename is a char*. I changed it to char[127] and it works great. Thanks!

    I still don't understand why it works, because the printf was displaying the filename correctly. Yet, when it was passed to fopen, it couldn't do anything with it.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You had undefined behaviour. It could have worked, and sort of did -- but it could also blow up on you at any time. Don't rely on this sort of thing working, or not working, or even behaving the same way when you run the program again.

    As for what happened? When the variable was a char*, it pointed to some random location in memory. This location was evidently writable, because you could assign your filename to it. But fopen() presumably used the same memory, because you didn't declare the memory as yours. It was unallocated.

    Actually, on a guess, the char* might have been pointing at or near the FILE* variable. It could overwrite the data without seg faulting, because the space was actually yours, but when fopen() set the FILE* pointer to NULL, the string got truncated prematurely.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM