Thread: Reading out of and writing into the same file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Reading out of and writing into the same file

    I having a strange problem when I try and read out of a file, and then write back into it.

    In one function, I'm reading it using:

    Code:
    FILE *fp = fopen(args[1],r);
    and then using x = fgetc
    but later on in another function I'm doing

    Code:
    FILE *fp1 = fopen(args[1],w);
    and then writing to it
    These file pointers are in seperate functions and neither one of them gets passed to the other function.

    The problem is when I try to run it, it crashes, and the only way to solve the problem is to then change both the 'r' and 'w' to 'rw'

    For some reason this then means that I can't seem to write to the file at all.

    It's acting very strange, especially as the operations are done in seperate functions. It doesn't seem to like me having different file permissions from seperate pointers.

    Can anyone give me any ideas as to how this sort of issue is resolved, or where I'm likely to be going wrong?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Oh wait, just had a thought, is the reason for this possibly because I'm not closing the files using fclose() once I've finished with them?

    Obviously I'd check on my code but don't have access to a compiler atm!

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    when u try to open the file with the write mode it clears of all the contents. And u are trying the open the file again with the read more. Which basically means u are trying to read a empty file. What u could do is u can try opening a file using "a+" mode which basically mean u open file on reading and appending condition.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    No I was trying to read it and then write to it(clear the previous contents)

    Will have to see tomorrow if it was due to me forgetting to fclose(fp)

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So is that something like u read the whole file in one fucntion ansd close that file. And agian open that file in another fucntion in write mode.

    Post your code lets have a look

    ssharish2005

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If fopen fails, use perror(args[1]) to tell you why.
    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.*

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fclose()ing your file pointer might solve the problem.

    You don't need two filestreams if you could use only one. Open a file in mode "r+", and rewind() the file after you've finished reading and want to start writing.
    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.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    look at the function definition for fopen.

    FILE *fopen(const char *FILE, const char *MODE);

    the second argument to fopen is string pointer.

    if you put r,w or rw the complier thinks it is a variable.

    what you need to do is put "r", "w" or "a" instead. for read, write and append.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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