[Last Call] Learn how to a build a cloud-first strategyRegister Now


reading file using streams

Posted on 2009-04-21
Medium Priority
Last Modified: 2012-05-06
Hello experts, i am trying to read and display some records from a text file (named books.txt) currently, for testing, i have put 5-6 records in it.
my program opens file and then reads, displays records 1 by 1.
but the problem is it stucks in endless loop and keeps displaying same values.
Pls Check.

Structure of record saved is :
Accession No, Subject, BookTitle, Author, price, Date of Purchase(Date, Month and year Seperately)

i am sending program, File(books.txt), OutPut (Wrong output produced)

Program :


struct date
      int d,m,y;
} ;

struct book
      int accno;
      char subject[30], title[30],  author[20];
      int price;
      date dop;
      int code ;
      char cat[20], type[20];
void ShowList(struct node*);
int Search(struct node *s, int accno);

void main()
      //open file to read records.

      //open file in read mode;
      ifstream fi("Books.txt");

       cout<<"\nShowing recrods from file : \n";
            //read data from file
            fi>>bk.price ;

            //show on screen.
            cout<<bk.accno<<"  "<<bk.subject <<"  "<<bk.title<<"  ";
            cout<<bk.author<<"  "<<bk.price<<"  " ;
            cout<<bk.dop.d<<"  "<<bk.dop.m <<"  "<<bk.dop.y ;

/*progrm ends */

Sample File :
101 OS LearnOS  Sood    1200 10 10 2009
104 VB VBInADay KiranGupta 450 12 12 2009
103 C++ LearnC++ SandeepSood 25 22 2010
102 C LearnC    Sandeep 450  20 10 2008
105 DS DS_Made_Easy Hardeep_Singh 500 11 11 2006

Output produced :

Showing recrods from file :

101  OS  LearnOS  Sood  1200  10  10  2009
104  VB  VBInADay  KiranGupta  450  12  12  2009
103  C++  LearnC++  SandeepSood  25  22  2010  102
103        25  22  2010  102
103        25  22  2010  102
103        25  22  2010  102
103        25  22  2010  102
(and so on
interruped using ^+Pause )
Question by:soodsandeep
  • 3
LVL 53

Expert Comment

ID: 24194303
Try using :


instead of :

>>       while(!fi.eof())

so that it covers error states too.
LVL 53

Expert Comment

ID: 24194329
And you'll have to fix your input file too, as this line :

>> 103 C++ LearnC++ SandeepSood 25 22 2010

is missing an integer value at the end (it has only 3 instead of the 4 that the other lines have).
LVL 53

Accepted Solution

Infinity08 earned 200 total points
ID: 24194377
That should get it to run ...

Now, I would suggest using a more robust way of getting input. Getting input directly from the stream using the operator>> is asking for trouble (just like you noticed now), as it's not at all resistant to problems with the input data. It'll work fine as long as the input data is exactly as expected, but as soon as something is out of the ordinary, the input stream goes into an error state, leaving you to clean up the mess.

If you instead use getline to read one line of input into a string :


and then get the appropriate values out of that string, then the input stream will stay clean, even if some of the lines contain errors.

Author Comment

ID: 24197474
ok. Infinity08, Thanks for your replies.
i will try all these and come back.
Thanks a lot.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question