Link to home
Start Free TrialLog in
Avatar of soodsandeep
soodsandeepFlag for India

asked on

reading file using streams

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 :

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>

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];
}bk;
 
void ShowList(struct node*);
int Search(struct node *s, int accno);

void main()
{
      
      //open file to read records.
      clrscr();

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


       cout<<"\nShowing recrods from file : \n";
      while(!fi.eof())
      {
            //read data from file
            fi>>bk.accno;
            fi>>bk.subject;
            fi>>bk.title;
            fi>>bk.author;
            fi>>bk.price ;
            fi>>bk.dop.d;
            fi>>bk.dop.m;
            fi>>bk.dop.y;

            //show on screen.
            cout<<"\n";
            cout<<bk.accno<<"  "<<bk.subject <<"  "<<bk.title<<"  ";
            cout<<bk.author<<"  "<<bk.price<<"  " ;
            cout<<bk.dop.d<<"  "<<bk.dop.m <<"  "<<bk.dop.y ;
      getch();
      }
getch();
}

/*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 )
Avatar of Infinity08
Infinity08
Flag of Belgium image

Try using :

           while(fi.good())

instead of :

>>       while(!fi.eof())

so that it covers error states too.
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).
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of soodsandeep

ASKER

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