Problem with fgets....

This is a discussion on Problem with fgets.... within the C Programming forums, part of the General Programming Boards category; i got a problem with this code... i cant find the problem so i posted here... the problem is that ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    Problem with fgets....

    i got a problem with this code...
    i cant find the problem so i posted here...
    the problem is that this program only reads EVERY OTHER LINE of a file...
    im new to file handling, sorry....

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *file = fopen("Data/Strings.txt", "a+");
    
    	char string[100];
    
    	clrscr();
    
    	if(file == NULL)
    	{
    		printf("\n\nError creating/reading file.\n");
    		printf("The directory/file may not exist.\n");
    
    		getche();
    		return 0;
    	}
    
    	else
    	{
    		printf("\n\nFile was created/read successfully.\n");
    		printf("\nEnter a sentence to be appended to the file:\n\n");
    		gets(string);
    
    		fputs(string, file);
    		fputs("\n", file);
    
    
    	rewind(file);
    
    	printf("\n\nThe contents of Strings.txt is:\n\n");
    
    	while(fgets(string, 100, file) != NULL)
    	{
    		printf("%s", fgets(string, 100, file));
    	}
    
    	fclose(file);
    
    	}
    
    	getche();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    sorry for the double post but...

    if i substitute this [while(!feof(file))] i get it right... though it has a "(null)" printed at the end...
    i still want to know what went wrong in the original code...

    thanks in advance

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Quote Originally Posted by Huskar
    the problem is that this program only reads EVERY OTHER LINE of a file...
    That is because you are calling fgets() twice on each iteration. You probably wanted to write:
    Code:
    while(fgets(string, 100, file) != NULL)
    {
        printf("%s", string);
    }
    Last edited by laserlight; 03-29-2009 at 10:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    ahh i see... thanks miss!
    one last thing...

    which is better?
    Code:
    while(!feof(file))
    {
         printf("%s", fgets(string, 100, file));
    }
    or the one you wrote? [the revised one?]

    thanks again

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Quote Originally Posted by Huskar
    which is better?
    Read Why it's bad to use feof() to control a loop and you'll be able to work out the answer for yourself
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    nb. that fgets returns a null pointer when it hits EOF
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. fgets problem
    By gambitmj in forum C Programming
    Replies: 5
    Last Post: 02-26-2002, 07:55 AM

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