reading file using streams

Posted on 2009-04-21
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.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
    LVL 53

    Expert Comment

    Try using :


    instead of :

    >>       while(!fi.eof())

    so that it covers error states too.
    LVL 53

    Expert Comment

    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

    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

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

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now