Thread: File problem!

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

    Exclamation File problem!

    Hey guys! Could you help me here?

    I have a Data Base.txt file. It contains:

    1. First question
    answer1
    answer2
    answer3
    answer4
    2. Second question
    answer1
    answer2
    answer3
    answer4

    and I have a structure in which I want to put the data from a file.
    struct questions
    {
    char questions[MAX], answers[4][MAX];
    } q[2];
    I have used the code below, but I got nothing, just a black screen.

    Code:
    #include<fstream.h>
    #include<stdlib.h>
    int main()
    {
      int i,j;
      ifstream fin;
       fin.open("Data Base.txt",ios::in | ios::nocreate);
    	if(!fin)
    		cout<<"Error reading file";
    
    	while(!fin.eof())
    	{
    		for(i=0;i<2;i++)
    		{
    			fin.get(q[i].questions,MAX);
    			for(j=0;j<4;j++)
    				fin.get(q[i].answers[j],MAX);
    		}
    	}
    
        fin.close();
    MAX is a defined constant.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well maybe I am missing something from this code but according to what I see, you WON'T see anything unless there is an error in reading the file....what were you expecting?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What are you expecting to get with a program whose only output is an error message that might not be issued?
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User Makaila's Avatar
    Join Date
    Oct 2009
    Posts
    10
    Well this is just a part of the code... further on I have a loop that write questions and answes... but there is nothing. So I was wondering is my code for reading from a file to a structure correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM

Tags for this Thread