Thread: Reading a file not working

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Reading a file not working

    This is probably pretty simple... This is my first year of dedicated learing C++ though, so give me some slack please. :P Well, i'm basically making a checkbooking program to create an example of using <fstream> to get better at it and such. I have most of my program working, but the error I have is when I try to read the file. (I'm storing information to a file to allow it to save for the next time I run the program.) Ive narrowed the error to my reading file function... Here's the code:
    Code:
    void display_transactions()
    {
    	ifstream fin;
    	string book;
    
    	fin.open("money.text");		// Opens record file
    	do{				// Loop to be sure to read the WHOLE file
    	     fin >> book;  // Saves the line
    	     cout << endl << book << endl;  // Prints the line
    	}while(fin.eof() == FALSE);
    
    	fin.close();	// Closes file
    }
    I would appreciate any ideas on how to fix this... Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What's the error? You shouldn't use .eof(), you should just do
    Code:
    while (fin >> book)
        cout << endl << book << endl;

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Well, when it gets to that portion of the program, it runs an infinite loop of blank lines... The .eof() is supposed to stop the loop when it reaches the end of the file from what I understand (given the parameters of the loop)...

    Well, I tried your code and it didn't work. Didn't have an infinite loop though.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Fun fact: an invalid file stream will not have .eof set. I would be surprised if your filename extension was actually ".text" instead of ".txt".

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Oh. That was quite a typo. Thank you very much.

    And yes that fixed it. It works perfectly the way I want it to now. Thank you!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hooray for working!

    Two other things:
    1) Your original code would have printed the last item in the file twice (.eof() is not set until after a read fails).

    2) "false" exists in the C++ language as a keyword, so there's no reason to define your own FALSE macro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

Tags for this Thread