Thread: fgets problem (i think)

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    fgets problem (i think)

    Im remaking a quiz-app i made a long time ago...
    heres a piece of the code
    Code:
    int read_quiz_questions()
    {
    	int error;
    	char question[BUFSIZ];
    	char question_header[]="/*question*/\n";
    	FILE *quiz_question;
    	quiz_question=fopen("question.txt","r");
    	if(quiz_question==0){
    		perror("\n");
    		printf("Cant read questions from file.\n");
    		printf("Press a key to exit.\n");
    		error=0;
    		getch();
    		exit(1);
    		}
    	else{
    			fgets(question,BUFSIZ,quiz_question);
    			if(strcmp(question,question_header)==0){
    				do{
    					if(fgets(question,BUFSIZ,quiz_question)==0){
    												printf("All questions are solved.\n");
    																		}
    					printf("%s\n",question);
    					}
    					while(fgets(question,BUFSIZ,quiz_question)!=NULL);
    														}
    			error=1;
    			}
    	return error;
    }
    As you can see it takes input from a file called question.txt.
    The file question.txt looks like this.
    Code:
    /*question*/
    Who am i?
    where do i live?
    Where did the previous question went???
    Normally my code would give as output this.
    Code:
    Who am i?
    where do i live?
    Where did the previous question went???
    But its giving this as output.
    Code:
    Who am i?
    Where did the previous question went???
    So somehow the second question dropped out... (wich is on the 3d line of the file).
    I alrdy tried to clear the buffer after fgets took the /*question*/
    as input.
    But that didnt worked.
    So i dont really know what i should try/do next.
    Suggestions are welcome

    ::edit::
    sry bout the stupid indentation... its always the same in my compiler it looks fine. But if i paste it in here it get messed up. Somehow the code, /code thing throws in spaces at certain places.
    Last edited by GanglyLamb; 03-19-2003 at 10:12 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You do a double read of the file, tossing aside one of the reads.
    Code:
                do {
                    if ( fgets(question,BUFSIZ,quiz_question)==0 )
                    {
                        printf("All questions are solved.\n");
                    }
                    printf("%s\n",question);
                } while ( fgets(question,BUFSIZ,quiz_question)!=NULL );
    Try this instead.
    Code:
                char *result;
                do {
                    result = fgets(question,BUFSIZ,quiz_question);
                    if ( result==0 )
                    {
                        printf("All questions are solved.\n");
                    }
                    printf("%s",question);
                } while ( result!=NULL );
    Or maybe this.
    Code:
                while(fgets(question,BUFSIZ,quiz_question) != NULL)
                {
                    printf("%s",question);
                }
                printf("All questions are solved.\n");
    >sry bout the stupid indentation... its always the same in my compiler it looks fine. But if i paste it in here it get messed up. Somehow the code, /code thing throws in spaces at certain places

    Indent with spaces instead of tabs.
    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
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hey, its kinda an fgets problem but not really, a logical one I guess. You never print "where do i live?".
    Code:
    /* The first time this executes we read "who am i?" */
    if(fgets(question,BUFSIZ,quiz_question)==0){
    
    /* Ok then we print it */
    printf("%s\n",question);
    
    /* Then we read "where do i live?" */
    while(fgets(question,BUFSIZ,quiz_question)!=NULL)
    
    /* Then we read "where did the previous question went???" */
    if(fgets(question,BUFSIZ,quiz_question)==0){
    
    /* Then we print it */
    printf("%s\n",question);
    
    /* Notice we Never printed "where do i live?" */
    To fix this, you can simplify it like
    Code:
    fgets(question,BUFSIZ,quiz_question);
               if(strcmp(question,question_header)==0){
    	      while(fgets(question,BUFSIZ,quiz_question)!=NULL)
    					printf("%s", question);
    			}
    I hope that makes sense, goodluck.
    EDIT - sorry didn't see dave's post, but yae what he said heh. Posted 1 minute before me

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Hey thx Dave_sinkula and Source-code.
    After i posted i realized it had to be some wrong thinking code.
    Anyway thx now i can go on with everything.

    About the indentation: i find tab indentation much easier when coding... but ill keep it in mind when i post another piece of code.

    C y'all

Popular pages Recent additions subscribe to a feed

Similar Threads

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