Thread: problem re-opening file

  1. #1
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53

    problem re-opening file

    I've been trying to solve this problem for a while

    Code:
    char templine[256],filename[256];
    .
    FILE *ofp;
    ofp=fopen(filename,"r");
    .
    .
    .
    fclose(ofp);
    ofp=fopen(filename,"w+");
    fprintf(ofp,"%s\n",templine);
    for some reason the file does not close and reopen correctly. By ading lots of printf statements, after the line
    ofp=fopen(filename,"w");

    ofp is NULL. however filename is unchanged from the initial opening. This means that the subsequent printf statments will obviously fail.

    I don't see what the problem is here


    the situation is that I've read out the contents of ofp, editing them on the way, and placed it all in a temp file. Now I just need to clear ofp and copy everything back in. (which is why I'm reopening it, as openeing in "w" clears the file). if there's a more reliable/cleaner way to do this please tell me. sorry if this is something obvious I've overlooked.

    thanks
    -Mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe your system can provide you with a reason if you add something like this.
    Code:
    ofp=fopen(filename,"w+");
    if(ofp != NULL)
    {
       /* write to the file */
    }
    else
    {
       perror(filename);
    }
    You might also check the return value of fclose.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    using the line:

    printf("%d\n",fclose(ofp));

    fclose is returning 0 (successful).


    the perror statement gives "permission denied".

    I'd assume this means the file ir read-only or already open, but as the file was successfully closed, this leaves me a little confused


    thanks
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  4. #4
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    oops...

    it is because the file was read only... I just got confused over which file was actually being opened and didn't realise.

    Thanks for helping me get that sorted out

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM