#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    writing to a file- overwriting old contents


    I am trying to have a process do the following once a second, for 5 seconds:
    1)open a file myFile.txt
    2) write its pid and value of 'i' to the file
    3)close the file

    The problem is that only the last round of the for loop shows up in the file, since the previous lines get overwritten. So, the file ends up with only 1 line instead of 5:

    'My pid is 1234. i is: 4'

    Could someone please tell me what I'm doing wrong?
    Here is my code:

    FILE *fileA;

    for(int i=0; i<5; i++){
    if( NULL == (fileA = fopen( "myFile.txt", "w")))
    perror("fopen failed");
    fprintf( fileA, "My pid is %d i is: %d\n", getpid(),i);
    fclose( fileA ); //close A.txt


    sleep(1);
    }
  2. #2
  3. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    8
    Rep Power
    0
    You are opening the file with the "w" mode which writes. This is fine, but you should open it in append mode, "a" which will not overwrite your original...

    BUT...

    Why are you opening and closing the file within the loop? Couldn't you open it outside the loop, write several times in the loop, then close it outside the loop? I may not understand what you are trying to do, but I think that would be valid. Well, anyway, hope this helps some.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    The character string mode specifies the type of access requested for the file, as follows:

    "r"

    Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

    "w"

    Opens an empty file for writing. If the given file exists, its contents are destroyed.

    "a"

    Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

    "r+"

    Opens for both reading and writing. (The file must exist.)

    "w+"

    Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

    "a+"

    Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    Thanks for the help. Now it does what I wanted.
    p.s. I am opening and closing the file within the loop only because my teacher specifically said to do it that way for the assignment. There will be a semaphore on the file and several other processes writing to it...

IMN logo majestic logo threadwatch logo seochat tools logo