Replacing a text file in C

This is a discussion on Replacing a text file in C within the C Programming forums, part of the General Programming Boards category; I have a text file with the following lines written on it: CPU 10000 CPU 9000 CPU 8000 CPU 7000 ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Replacing a text file in C

    I have a text file with the following lines written on it:
    CPU 10000
    CPU 9000
    CPU 8000
    CPU 7000
    CPU 6000

    In C++, if I open a file just for writing, whatever was on it is lost and the file is updated with the new content. I'm trying to do the same thing in C with fopen("file'txt", "w") but the file is not being updated. Should I use something else in the place of w? Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    Using fopen() with mode "w" is correct. If the file isn't updating then there must be something wrong in the rest of your code.
    If you understand what you're doing, you're not learning anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends. w is for write. Not update:
    w
    Truncate file(1,n) to zero length or create text file(1,n) for writing.
    The stream is positioned at the beginning of the file.
    It depends on the OP's definition of "update".

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Another question. This one is tricky for me...
    If I try to open a file and it doesn't exist, my FILE pointer will be equal to NULL. Do I have to close the first stream before opening a new one? Here's the situation:
    Code:
    points = fopen("points.txt", "r");
    if(points == NULL)
    	create_points_file();
            /* should I fclose(points) here? */
    
    points = fopen("points.txt", "r");
    while(fgets(line, sizeof(line), points) != NULL)
    	printf("%s", line);
    fclose(points);

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Because if you try to open something that isn't there, fopen has failed, and has opened nothing. However, with the second fopen, you still should be testing to make sure it isn't null (ie: fopen has not failed).

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hmm. I read caduardo21's post differently.
    Quote Originally Posted by caduardo21
    Another question. This one is tricky for me...
    If I try to open a file and it doesn't exist, my FILE pointer will be equal to NULL. Do I have to close the first stream before opening a new one? Here's the situation:
    Code:
    points = fopen("points.txt", "r");
    if(points == NULL)
    	create_points_file();
            /* should I fclose(points) here? */
    
    points = fopen("points.txt", "r");
    while(fgets(line, sizeof(line), points) != NULL)
    	printf("%s", line);
    fclose(points);
    Essentially, if it opened, close it.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "myfile.txt";
       FILE *file = fopen(filename, "r");
       if ( !file )
       {
          printf("creating %s\n", filename);
          file = fopen(filename, "w");
          if ( file )
          {
             int i;
             for ( i = 0; i < 5; ++i )
             {
                fprintf(file, "data #%d\n", i + 1);
             }
             fclose(file);
          }
       }
       file = fopen(filename, "r");
       if ( file )
       {
          char line[80];
          while ( fgets(line, sizeof line, file) )
          {
             fputs(line, stdout);
          }
          fclose(file);
       }
       else
       {
          puts("WTF???");
       }
    
       return 0;
    }
    
    /* my output
    C:\Test>Test
    creating myfile.txt
    data #1
    data #2
    data #3
    data #4
    data #5
    
    C:\Test>Test
    data #1
    data #2
    data #3
    data #4
    data #5
    */
    [edit]Standing corrected by the subsequent post.
    Last edited by Dave_Sinkula; 05-27-2005 at 05:23 PM.
    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.*

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    One problem: The file is opened twice against the same File ptr if it already exists.
    The test only ran against "no existing file".

    To correct: move the second readmode open inside the (!file) braces immediately after the
    fclose of the newly created file.

    The program should run ok the way it is, but you will have left an inaccessible open file until
    the programs closes. In this case that doesn't mean much - just bad practice.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    D'oh! If I'd only heed my own advice. Yet another possible way(?).
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "myfile.txt";
       char line[80];
       FILE *file;
       while ( (file = fopen(filename, "r")) == NULL )
       {
          printf("creating %s\n", filename);
          file = fopen(filename, "w");
          if ( file )
          {
             int i;
             for ( i = 0; i < 5; ++i )
             {
                fprintf(file, "data #%d\n", i + 1);
             }
             fclose(file);
          }
       }
       while ( fgets(line, sizeof line, file) )
       {
          fputs(line, stdout);
       }
       fclose(file);
       return 0;
    }
    But you may want to double-check!
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21