Thread: FileIn question

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    FileIn question

    This is my program wondering why it shows a blank data at the end
    any clues?
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
    		char BookTitle[20];
    		char Author[15];
    		char Cost[10];
    		char Pages[11];
    
    		ifstream FileIn("authors.txt");
    
    		while(FileIn)
    		{
    			FileIn.get(BookTitle, 20, ',');
    			FileIn.get();
    			FileIn.get(Author, 15, ',');
    			FileIn.get();
    			FileIn.get(Cost, 10, ',');
    			FileIn.get();
    			FileIn.get(Pages, 11, ',');
    			
    		
    			cout <<"Book Title: "<< BookTitle << endl;
    			cout <<"\n" <<"Author: "<< Author << endl;
    			cout <<"\n" <<"Cost: " << Cost << endl;
    			cout <<"\n" <<"# of Pages: "<< Pages << endl;
    			cout <<"*******************************************"<< endl;
    		}
    
    		return 0;

    and my output looks like this why does it print out when there is no more info?


    Book Title: Gone With The Wind

    Author: M. Mead

    Cost: $19.00

    # of Pages: 384 pages
    *******************************************
    Book Title:
    Bones To Dust

    Author: H. Hoffman

    Cost: $21.00

    # of Pages: 174 pages
    *******************************************
    Book Title:
    Get This File

    Author: M. Mcardle

    Cost: $29.99

    # of Pages: 234 pages
    *******************************************
    Book Title: //why is this here???????

    Author:

    Cost:

    # of Pages:
    *******************************************
    Press any key to continue

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    probably because FileIn is still valid after the last data field is read in because it hasn't found the EOF yet. Therefore the loop has to be done one more time. But what to put in the variables? In this case the program isn't putting anything. Often it will put a duplicate set of values for the last set of valid data reads, but you can't count on it. Try making

    FileIn.get(BookTitle, 20, ',');

    as the conditional of the loop, and put another call to

    FileIn.get();

    after

    FileIn.get(Pages, 11, ',');

    to be sure you clear the last comma after the last data read gets cleared out sowhen get() trys to read EOF at the very end it will fail causing the loop body to be bypassed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM