Thread: Bit of help with File I/O

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    Bit of help with File I/O

    Okay guys. Hello again! I have yet another question about the login program i've been working on.

    I have the program working quite nicely. It uses a class to register students and has a data value that is set to either 1 or 0 depending on if they are logged in or not.

    Now, here's what I could use a bit of help with.

    I would like to use a text document to store all the data entries in, which is no problem for me to do. I would have it saved in the file as the following format:

    Firstname Lastname UserIDNumber
    Firstname Lastname UserIDNumber
    Firstname Lastname UserIDNumber
    etc.

    Simple enough. My problem is this; based off of tutorials and everything I've seen and read, I can't figure out how I would go about loading it back into my class. How would I go about reading the information into char[20] arrays for each item(firstname, lastname, useridnumber)?

    From tutorials i've seen, it always just shows loading a whole text file in a string buffer thingy, never individual items like I need. I know it's possible, I just dont know how! LoL.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How would I go about reading the information into char[20] arrays for each item(firstname, lastname, useridnumber)?

    When you write out the data into the text file, what do you use to separate each value? It looks like you separate them with spaces. If so, you can read them in almost the same way you would read them in from a user. If you had an ifstream named fin, for example, you could put fin >> firstname into a loop and it would work just like cin >> firstname.

    >> has a data value that is set to either 1 or 0 depending on if they are logged in or not.

    C++ has a type called bool that takes a true or false value that is more clear than using 1 or 0.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Ok, I made a quick program to try to do that...it compiles and runs fine...but wont put out the information. What did I do wrong?

    Code:
    #include <fstream>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    	char fname[20];
    	char lname[20];
    	char idnum[10];
    
    	ifstream file;
    	file.open("info.txt",ios::in);
    
    	if(!file)
    	{
    		cout << "Unable to open!";
    		goto end;
    	}
    		
    	while(file)
    	{
    		file >> fname >> lname >> idnum;
    	}
    
    end:
    	file.close();
    	cout << "Firstname - " << fname;
    	cout << "\nLastname - " << lname;
    	cout << "\nID Num - " << idnum << "\n\n";
    
    	return 0;
    }
    
    //Textfile named 'info.txt'
    //Contents of text file called 'info.txt':
    /*
    Justin Allen 01103657
    */

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Edit: Actually, it looks like my original thought might not work either... Apparently my compiler empties out the fname variable even if the read fails. The solution would be to read into a temporary variable and copy it to the permanent variable inside the while loop.


    Normally, you should put the read into the control of the while loop. The stream fails only after you attempt to read past the end of the file, so your code runs the loop two times, emptying the data on the second run when there is nothing in the file. For testing you can use
    Code:
    char temp[20];
    while(file >> temp)
    {
        strcpy(fname, temp);
        file >> lname >> idnum;
    }
    Obviously, when you have more than one entry in your text file, you would actually save the data you read in during the body of the while loop.

    BTW, if you are learning C++, you should probably be using C++ strings, which as it happens don't have this issue and are easier to copy in general.
    Last edited by Daved; 03-09-2006 at 06:00 PM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Actually, i solved it my own way; Moved the cout's into the loop, and changed while(file) to while(!file.eof())

    And, Im sorta learning c++, ive got most of the stuff down pretty well(pointers, classes, structs, linked lists, etc.). Now im trying to become functional with it, as practice excersizes don't do much good in the real world.

    And i was going to use strings...but in my porgram there was some sort of reason that I chose character arrays instead.... Not that i remember what that reason was....
    Last edited by KoshiB; 03-09-2006 at 06:08 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    goto end;
    Don't ever use goto's. In your program, you can accomplish what you want to do using a simple if-else statement:
    Code:
    if(!file)
    {
    	cout << "Unable to open!";
    	//goto end;
    }
    else
    {
    	while(file)
    	{
    		file >> fname >> lname >> idnum;
    	} 
    }
    //end:
    Actually, i solved it my own way; Moved the cout's into the loop, and changed while(file) to while(!file.eof())
    Bad solution. There are errors that can happen while reading from a file which will not allow you to read from the file anymore, yet you haven't read the end of the file, so you will never read the end of the file, and your while loop will be an infinite loop.
    Last edited by 7stud; 03-09-2006 at 06:43 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    I don't normally use goto's, and indeed, have since removed them.

    Heres the program in current state:

    Code:
    #include <fstream>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    	char fname[20];
    	char lname[20];
    	char idnum[10];
    
    	ifstream file;
    	file.open("info.txt",ios::in);
    
    	if(!file)
    	{
    		cout << "Unable to open!\n\n";
    		system("pause");
    		cout << endl;
    		return 0;
    	}
    		
    	while(!file.eof())
    	{
    		file >> fname >> lname >> idnum;
    		cout << "Firstname - " << fname;
    		cout << "\nLastname - " << lname;
    		cout << "\nID Num - " << idnum << "\n\n";
    		
    	}
    
    	file.close();
    	
    	return 0;
    }
    
    //Textfile named 'info.txt'
    //Contents of text file called 'info.txt':
    /*
    Justin Allen 01103657
    */
    How would you change it to not use the eof?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem with using the eof() is exactly the same as the original problem, the loop runs one extra time. Moving the couts into the loop allowed you to see the data being output, but the loop still runs an extra time. Putting the (file >> fname) into the while control is how you get rid of the eof(), and that will now work since the cout's are inside the loop. That was actually my original suggestion, but because the cout's were outside the loop, it didn't work in your original code. It will get rid of the extra, blank output at the end.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Actually, the loop doesn't run an extra time with it. Otherwise, it would spit out an extra set of the cout's with blank information, like it did previous to my change, but it doesn't. It works fine now, at least using my compiler...

    the method of putting file >> fname >> lname >> idnum in the while() loop does work though. Is this the best way to do this?
    Last edited by KoshiB; 03-09-2006 at 07:13 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Otherwise, it would spit out an extra set of the cout's with blank information, like it did previous to my change, but it doesn't.

    Ok, with my setup it does print out an extra set of blank information. My guess is that you do not have a trailing newline at the end of your input file.

    Putting the read into the while loop is the best way because it handles both cases. Basically what it does is the read method (in this case operator>>) returns the stream, which is converted to true if the stream is in a good state, and false if it is in a fail state. So if the read fails, it will evaluate to false and your loop will not execute. The difference with eof() is that if there is a newline at the end of the file (which is common), then the eof() will be false after you read the last entry because there will still be that last newline in the file that has not been read.

    So press enter at the end of the line of data in your input file and then try both methods. You will probably get the blank information with the eof() method, but not with the other one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM