A program gone mad :)

This is a discussion on A program gone mad :) within the C Programming forums, part of the General Programming Boards category; Could somebody tell me is this the correct way to read from a txt file? this is the content of ...

  1. #1
    Registered User Makaila's Avatar
    Join Date
    Oct 2009
    Posts
    10

    A program gone mad :)

    Could somebody tell me is this the correct way to read from a txt file?

    this is the content of a file:

    1. Which one isn't for Object oriented programming? // question
    1) Java
    2) C++
    3) Visual Basic
    4) C // 4 answers
    4 // the number of the correct answer
    2. Which one is the color model for computers?
    1) PDF
    2) DBMS
    3) RGB
    4) USD
    3

    and I need to put this data to a structure
    struct millioner
    {
    char question[MAX], answers[4][MAX];
    int correct;
    };

    and I've used the following code to read this from a file:
    Code:
    FILE *ptr;
    struct millioner q[BROJ];
    char right[MAX];
    int i,j;
    
    ptr=fopen("Data base.txt","r");
    
    	if(ptr==NULL)
    		messages(0,0);
    
    	while(!feof(ptr))
    	{
    		for(i=0;i<2;i++)
    		{
    			fgets(q[i].question,MAX,ptr);
    				for(j=0;j<4;j++)
    				fgets(q[i].answers[j],MAX,ptr);
    			fgets(correct,MAX,ptr);
    			q[i].correct=atoi(right);
    			
    		}
    	}
    At first this was working just fine and now, the program always takes answer No 2 as correct, although the correct answer to for example, a question 1 is under number 4.
    I have checked the number that gets put into a structure element q[i].correct and it is always 2. But I wrote different in a txt file.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Makaila View Post
    Could somebody tell me is this the correct way to read from a txt file?

    this is the content of a file:

    1. Which one isn't for Object oriented programming? // question
    1) Java
    2) C++
    3) Visual Basic
    4) C // 4 answers
    4 // the number of the correct answer
    2. Which one is the color model for computers?
    1) PDF
    2) DBMS
    3) RGB
    4) USD
    3

    and I need to put this data to a structure
    struct millioner
    {
    char question[MAX], answers[4][MAX];
    int correct;
    };

    and I've used the following code to read this from a file:
    First, let's change the indentation to make it look like, what it actually is:


    Code:
    FILE *ptr;
    struct millioner q[BROJ];
    char right[MAX];
    int i,j;
    
    ptr=fopen("Data base.txt","r");
    
    	if(ptr==NULL)
    		messages(0,0);
    
    	while(!feof(ptr))
    	{
    		for(i=0;i<2;i++)
    		{
    			fgets(q[i].question,MAX,ptr);
    			for(j=0;j<4;j++)
            			fgets(q[i].answers[j],MAX,ptr);
    	        	fgets(right,MAX,ptr);
    		        q[i].correct=atoi(right[0]);
    			
    		}
    	}
    OK, try that. It's off the cuff & untested. Post back if you have a problem. BROJ is a defined constant, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 09:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 10:39 AM

Tags for this Thread


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