about function "fgets"

This is a discussion on about function "fgets" within the C Programming forums, part of the General Programming Boards category; Hi everyone, I was working on alittle program, the program reads from a file and writes to another file by ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    about function "fgets"

    Hi everyone,
    I was working on alittle program, the program reads
    from a file and writes to another file by using the C library funtions
    fgets and fputs , the program reads well but when it outputs to the file it writes the message twice instead of only onne time can anyone tell me what I am getting wrong looking at the code below, I just cut some code to show what I have implemented...this is part of the code after the decleration I opend the file and then start processing.

    infile message is "helo" it writes twice to outfile

    Code:
    code
    
    #define max 1024
    char* p_box (char* orig_msg , char* msg_after_pbox ) ;
    
    int main (int argc, char *argv[])
    {
        FILE *infile, *outfile;
        //char ch;
        char str1 [max];
        char str2 [max];  /* temporary for output */
    
     while(!feof(infile))
        {
              fgets(str1,max-1,infile);
     
              p_box( str1, str2 );  
         
              fputs(str2,outfile);
              
        }
        fclose(outfile);
        fclose(infile);  
    }

    thanks in advance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    My guess is it is happening inside the p_box function.

    BTW: with fgets you can do:

    fgets(str1, max, infile);

    ...since it will be sure to stop at the appropriate point...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Yet again we have feof() misunderstanding, and yet again....


    Code:
    while( fgets(str1,max,infile) != NULL ) {
        p_box( str1, str2 );  
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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