writing what a program has read

This is a discussion on writing what a program has read within the C Programming forums, part of the General Programming Boards category; I have been able to read what are the contents of a file and display them on the screen, it ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    writing what a program has read

    I have been able to read what are the contents of a file and display them on the screen, it worked well

    Then i tried to write a new file (file1.dat) that contains the odd lines of the first file. But there is nothing that appears on file1.dat.

    Code:
      FILE * fp, * outptr;
      char lines [ 90 ];
    
      if ( ( fp = fopen ( FILENAME, "r" ) ) == NULL ) {
        fprintf ( stderr, "fileloop1: Unable to open %s!\n", FILENAME );
        exit ( 1 );
        }
    
      fgets (lines, 90, fp);
    
      while ( ! feof ( fp ) ) 
      {
         fputs ( lines, stdout ); 
         fgets ( lines, 90, fp ); //displaying the contents of a file 
       }
    
         if(feof(fp))
         {
            fgets(lines, 90, fp);
            outptr = fopen ("file1.dat","w");
            fprintf(outptr, "%s\n", lines); // trying to write the line of the first file in the second one
           }
    please help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Code:
         if(feof(fp))
         {
            fgets(lines, 90, fp);
    What do you expect this to do?

    If there is anything at all in lines, it's probably whatever was read last time fgets() was successful.

    See the FAQ about using feof() to control a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    thanks for your reply

    well, I changed the code a bit

    Code:
      while ( ! feof ( fp ) ) {
        outptr = fopen ("file1.dat","w");
        fputs ( buffer, stdout );
        fgets ( buffer, 81, fp );
        fprintf(outptr, "%s\n", buffer);
        }
    now, "file1.dat" shows some output, but it's the last line of the file and it misses the first 2 letters... what's happening ?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You still haven't read the FAQ regarding feof()!

    Listen to what Salem has said, for a start. You output 'buffer' before you even load anything into it...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    while ( fgets( buff, sizeof buff, fp ) != NULL ) fputs( buff, stdout );

    Is all you need to copy a text file to your terminal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Quote Originally Posted by Salem View Post
    while ( fgets( buff, sizeof buff, fp ) != NULL ) fputs( buff, stdout );

    Is all you need to copy a text file to your terminal.
    thanks but it's not what I all need.

    copying a file to a terminal, i've already been able to do it.

    but how about writing a new file that writes what the program has read ?

    Code:
    while ( fgets( buffer, sizeof buffer, fp ) != NULL ) fputs( buffer, stdout );  {
        outptr = fopen ("file1.dat","w");
        fprintf(outptr, "%s", buffer);
        }
    Why the file file1.dat is empty after every run of the program. Where did I mistake ?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by hinman View Post
    Code:
    while ( fgets( buffer, sizeof buffer, fp ) != NULL ) fputs( buffer, stdout );  {
        outptr = fopen ("file1.dat","w");
        fprintf(outptr, "%s", buffer);
        }
    Why the file file1.dat is empty after every run of the program. Where did I mistake ?
    Poor indentation strikes again.
    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
    Apr 2007
    Posts
    16
    Where did you see any problem with the identation of these 4 lines of code ??
    Code:
    while ( fgets( buffer, sizeof buffer, fp ) != NULL ) fputs( buffer, stdout );  {
        outptr = fopen ("file1.dat","w");
        fprintf(outptr, "%s", buffer);
        }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by hinman View Post
    Where did you see any problem with the identation of these 4 lines of code ??
    Code:
    while ( fgets( buffer, sizeof buffer, fp ) != NULL ) fputs( buffer, stdout );  {
        outptr = fopen ("file1.dat","w");
        fprintf(outptr, "%s", buffer);
        }
    Where's the loop body? And even if your loop body was correct, why continually reopen and truncate the file?
    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.*

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Quote Originally Posted by Dave_Sinkula View Post
    Where's the loop body? And even if your loop body was correct, why continually reopen and truncate the file?
    No it's not what i am trying to do, there are 2 files, the first one is file.dat the second is file1.dat

    File.dat has a list of 10 lines.

    When I do this:
    Code:
    while ( fgets( buffer, sizeof buffer, fp ) != NULL ) {
        fputs( buffer, stdout );
        outptr = fopen ("file1.dat","w");   // opening and creating  a new file
        fprintf(outptr, "%s", buffer);  // write in it lines of the first file
        }
    its output is the last line of the output, and it misses the first 2 letters, I don't know what to change in order to fix that.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Code:
     
      FILE * fp, * outptr, * outptr2;
      char buffer [ 81 ];
      int i;
      outptr = fopen ("file1.dat","w"); 
      outptr2 = fopen ("file2.dat","w");
     
      if ( ( fp = fopen ( FILENAME, "r" ) ) == NULL ) {
        fprintf ( stderr, "fileloop1: Unable to open %s!\n", FILENAME );
        exit ( 1 );
        }
     
       if ( ( outptr = fopen ("east.dat","w") ) == NULL ) {
         fputs("Unable to open east.dat",stderr);
         fclose(outptr) ;
    }
     
        while ( fgets( buffer, sizeof buffer, fp ) != NULL ) {
          fputs( buffer, stdout );
        }
     
        for(i=1; i<=10; i=i+2)
         {          
              fgets(buffer, 81, fp);
              fprintf(outptr, "%d: %s", i, buffer);
         }  
     
        for(i=0; i<=10; i=i+2)
         {          
              fgets(buffer, 81, fp);
              fprintf(outptr2, "%d: %s", i, buffer);
         }  
       fclose(fp);
       fclose(outptr);
       flclose(outptr2);
    The output of both file1.dat and file2.dat are empty, except the line numbers. Why doesn'it prints the lines ?

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > but how about writing a new file that writes what the program has read ?
    It's the same loop.

    It doesn't matter a bean whether you're reading from a FILE* fp of a file you've opened using fopen(), from a FILE* opened using popen(), or from stdin. fgets() works the same regardless.

    Likewise for output, the functions writing to a FILE* don't care whether it's stdout, stderr, a file or a pipe.

    Before you do anything with a file, you need to practice simply copying the "input" to the "output". Until you can do this reliably, there is no point worrying about what you're going to do as far as processing the data is concerned. Because sooner or later, you'll end up processing some garbage data, then wondering what the hell is going on. And it will all be down to erroneous use of files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 05:45 AM
  3. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 09:31 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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