Thread: File Handling?!?

  1. #1
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Unhappy File Handling?!?

    Ok, I asked this before. I've tried the suggestions and have failed. This is what I want to do.

    I have a question.txt file that looks like this.
    Question 1: Yadda yadda
    Answer: TRUE
    Question 2: Yadda Yadda
    Answer: FALSE

    Now how would I make a program read from that file, print the question, ask for user input then check the answer against the answer in the text file? I think I can figure out the user input part, but I just need help with the file handling part. Any help would be appretiated.


    ~~Twiggy~~

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A bit like this perhaps
    Code:
    #include <stdio.h>
    
    int main ( int argc, char **argv ) {
        FILE *fp = fopen( "question.txt", "r" );
        char question[BUFSIZ], answer[BUFSIZ], my_answer[BUFSIZ];
        while ( fgets( question, BUFSIZ, fp ) != NULL ) {
            fgets( answer, BUFSIZ, fp );
            printf( "%s", question );
            fgets( my_answer, BUFSIZ, stdin );
            if ( strcmp( answer, my_answer) == 0 ) {
                // correct
            } else {
                // wrong
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM