fgets moving on to a newline

This is a discussion on fgets moving on to a newline within the C Programming forums, part of the General Programming Boards category; im making a quiz right now and i have two txt files one with questions another with answers this is ...

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

    fgets moving on to a newline

    im making a quiz right now and i have two txt files one with questions another with answers
    this is a piece of my code
    Code:
    do{
            fgets(vraag,100,vragen);   /*neem een vraag uit vragen.txt*/
            printf("%s\n",vraag);      /*print de vraag*/
      
            fgets(antwoord,100,antwoorden); /*haal antwoord uit de file*/
            gets(antwoord_eigen);               /*scanf antwoord als een string*/                         
            
            if(strcmp(antwoord,antwoord_eigen)==0){   /*vergelijk de antwoorden uit txt en scanf*/
                                                 printf("Het antwoord is correct\n");}
            if(strcmp(antwoord,antwoord_eigen)==1){
                 printf("Het antwoord is verkeerd het juiste antwoord was:%s",antwoord);}
            }
            while(strcmp(antwoord_eigen,stop)==0); /*typt men stop dan stopt het programma*/
    sry for the probably unreadable language
    vraag=question and antwoord=answer
    anyway i manage to get one question out of the txt file and one answer out of the txt file but now i want to move on to the next line where the next question is and the next answer so i have to
    do something with fgets(vraag,dunno probably here something to go to the next line or...,vragen);
    and the same thing for the answers

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    gets(antwoord_eigen);
    Use fgets() - it will save you lots of trouble!

    Make sure your string is big enough to hold the biggest single line in the text file! If fgets() reaches the '\n' at the end of a line, it will read in the next line when you call it again.
    Last edited by Sargnagel; 11-05-2002 at 04:07 PM.

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    actually i just wanna clear evrything that is in the string antwoord en vraag en then i wanna move a line down in each txt file where the next question and next answer is ...
    mayb i should add all the files

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    actually i just wanna clear evrything that is in the string antwoord en vraag en then i wanna move a line down in each txt file where the next question and next answer is ...
    No, problem.
    Code:
    void clear_string(char *mystring, int size)
    {
        int i;
        for(i = 0; i < size; i++)
        {
            mystring[i] = '\0';
        }
    }
    @fgets(): look in my posting above ... I've edited it.
    Code:
    if(strcmp(antwoord,antwoord_eigen)==0){   /*vergelijk de antwoorden uit txt en scanf*/
        printf("Het antwoord is correct\n");}
    if(strcmp(antwoord,antwoord_eigen)==1){
        printf("Het antwoord is verkeerd het juiste antwoord was:%s",antwoord);}
    Remember: strcmp() returns 0 if the two strings match but it returns !=0 if the strings don't!
    (if string1 > string2 --> returns >0 and if string1 < string2 --> returns <0)

    It is not necessary to call strcmp() two times. You can use if ... else ...
    Code:
    if(strcmp(antwoord,antwoord_eigen)==0)
    {
        printf("Het antwoord is correct\n");
    }
    else
    {
        printf("Het antwoord is verkeerd het juiste antwoord was:%s",antwoord);
    }
    Last edited by Sargnagel; 11-05-2002 at 04:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading integers using fgets
    By larne in forum C Programming
    Replies: 3
    Last Post: 10-15-2008, 12:36 PM
  2. Mac - Default Lines Endings - fgets() - no worky
    By Dino in forum C Programming
    Replies: 6
    Last Post: 01-30-2008, 11:59 PM
  3. Replies: 12
    Last Post: 10-17-2005, 07:49 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM

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