Thread: fgets problem (i think)

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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