a problem with reading from a FILE

This is a discussion on a problem with reading from a FILE within the C Programming forums, part of the General Programming Boards category; Hello! I have a problem to read data from a Database.txt file. The file contains the questions and answers for ...

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

    Exclamation a problem with reading from a FILE

    Hello!
    I have a problem to read data from a Database.txt file. The file contains the questions and answers for the quiz, and I have to read it from a file and put it to a structure so I can use it in program. I have done this with a binary file, but unfortunately I must use txt.

    this is an example of questions in txt file. Of course, there are more, but I'll put just 2 of them here.

    1. Koji programski jezik nije objektno orijentisan? //this is a question
    1)Java
    2)C++
    3)Visual Basic
    4)C //these 4 are the answers
    4 // and this is the number of the correct answer
    2. Sta je od navedenog model boja u racunaru?
    1)PDF
    2)DBMS
    3)RGB
    4)USD
    3

    now I will give you the structure I have made to put the data from the file in:
    Code:
    struct milioner
    {
    	char question[MAX], answers[4][MAX];
    	int correct;
    };
    
    // and I have tried to read the file and put it in a structure with this code, but it doesn't work
    
    FILE *ptr;
    struct milioner q[BROJ];
    int i,j;
    
    ptr=fopen("Database.txt","r");
    	if(ptr==NULL)
    		printf("Error reading file.");
    
    	if(!feof(ptr))
    	{
    		for(i=0;i<BROJ;i++)
    		{
    			fgets(q[i].question,MAX+1,ptr);
    			for(j=0;j<4;j++)
    				fgets(q[i].answers[j],MAX+1,pok_dat);
    			fscanf(ptr,"%d",q[i].correct);
    
    		}
    	}
    Thanks in advance!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    fgets(q[i].question,MAX+1,ptr);
    It is MAX not MAX+1. You generally put the size of the buffer (q[i].question).
    If you have MAX+1, it will read MAX characters and then add the '\0' character on the end. Since you have MAX characters in your buffer the program will do something bad.

    The other thing is that I am not sure fscanf will work like this. In general be careful when mixing different input functions. I believe fscanf() will leave the newline character in the buffer, so the next fgets() function will read just that and exit.

    About feof() look at this and the FAQ. Combining that with using fscanf() and fgets() unexpected things might happen.

    My advice? Just use fgets() and atoi()

    And what is pok_dat in your code???

  3. #3
    Registered User Makaila's Avatar
    Join Date
    Oct 2009
    Posts
    10
    oh, pok_dat is the name for ptr in my program, I forgot to rename it to ptr for this thread.
    And, thanks a lot for the advice... It is ok now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Replies: 20
    Last Post: 06-13-2005, 12:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 07:52 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