Thread: FILE I/0 and Structures

  1. #1
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12

    FILE I/0 and Structures

    Hey;

    I'm in the process of building a program that requires the use of a .txt file, however the values have to be loaded into a structure. In the end, the load process has to be in it's own function but i'm not worried about that at the moment. Here is something that i've been messing around with:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    struct data
    {
    	char title[50];
    	char author[50];
    	int pages;
    };
    
    
    int main(void)
    {
    	int i;
                    /*used for testing*/
    	/*char title[20];
    	char author[20];
    	int pages;*/
    
    //	void openFile(struct data structure[]);
    
    	struct data structure[2];
    	
    	fstream in_file;
    
    	in_file.open("a:test.txt",ios::in);
    
    	if(!in_file)
    	{
    		cout<<" \nFile could not be found\nPress <ENTER> to exit..."<<endl;
    		getch();
    		exit(1);
    	}
    
    	
    	while(!in_file.eof())
    	{
    		i++;
    		in_file>>structure[i].title>>structure[i].author>>structure[i].pages;
    
    		//used for testing
    		/*in_file<<title<<author>>pages;
    		cout<<" \n"<<title<<"\n"<<author<<"\n"<<pages<<endl;*/
    		in_file.get();
    	}
    
    	in_file.close();
    
    	cout<<"\n\n"<<structure[0].title
    		<<"\n"<<structure[0].author 
    		<<"\n"<<structure[0].pages
    		<<endl;
    
    	getch();
    
    return 0;
    }
    Another problem that i'm running in to is that everytime i run the program, it creates the file, i don't want this to happen. I want my file check routine to kick in if the file isn't there. The file contains:
    Book_Title Author 56

    I'm pretty sure that the probem is in logic somewhere, but i just can see it.

    Thanks in advanced;
    S0n1C!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Another problem that i'm running in to
    Hmmm...I must have missed the first one.

    1)
    Code:
    #include <iostream.h>
    #include <conio.h> //get rid of this
    #include <stdlib.h> //get rid of this
    #include <fstream.h>
    should be:
    Code:
    #include <iostream>
    #include <fstream>
    and change this:
    Code:
    getch();
    to:
    Code:
    cin.get();
    2)
    Code:
    int main(void)
    should be:
    Code:
    int main()
    3) Don't use eof() as the means to terminate reading from the file. That can cause problems with infinite loops; it is not as straightforward as it appears. Instead, to properly terminate loops that read from files, you should use your read statement as the while loop conditional, e.g.:
    Code:
    while(  in_file>>structure[i].title>>structure[i].author>>structure[i].pages )
    4)
    Another problem that i'm running in to is that everytime i run the program, it creates the file, i don't want this to happen.
    Huh? open() fails if it can't find the file. Where is the program creating a file?
    Last edited by 7stud; 01-18-2007 at 11:10 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    while(!in_file.eof())
    	{
    		i++; //could be anything
    		in_file>>structure[i].title>>structure[i].author>>structure[i].pages;
    
    		//used for testing
    		/*in_file<<title<<author>>pages;
    		cout<<" \n"<<title<<"\n"<<author<<"\n"<<pages<<endl;*/
    		in_file.get();
    	}
    Don't control file input using eof().
    Also i looks uninitialized - could have any value (including 0).

    (Since you can't know how much data you are going to have, consider using std::vector<data>. Same goes for using std::string instead of char arrays - there's no telling how long a title may be.)

    To stop it from creating a new file, don't use fstream - use ifstream, which is an input stream and just fails if the file doesn't exist.

  4. #4
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    Thanks, I'm pretty new to C++, if you couldn't tell, so i'm still getting use to the new language. Thanks for the suggestions, and help. I was wondering how to assign values to variables without knowing the length of the string.

    Thank you for the help;
    S0n1C!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I was wondering how to assign values to variables without knowing the length of the string.
    Code:
    string str = "hello";
    int a = 10;  //assigned value to a variable w/o knowing the length of the string
    That should demonstrate that your question is non-sensical. Since you're new and don't really know how to talk about C++ yet, you should try to post code demonstrating what you mean.
    Last edited by 7stud; 01-18-2007 at 06:38 PM.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Wow, it was a new programmer who actually thanked us for correcting his mistakes! Way to go S0n1C!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    Why not thank you guys? I am a n00b and i know that. so i bow down to you and thank you again for answering my n00b questions.

    S0n1C!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling Array Of Structures From A File
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 09:59 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Replies: 3
    Last Post: 11-14-2005, 08:03 AM
  4. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  5. Structures and file io
    By mungyun in forum Linux Programming
    Replies: 1
    Last Post: 05-18-2002, 02:48 PM