infile.getline(); not reading text file

Hello,
I am trying to read and display the contents of the following text file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Harry Potter and the Deathly Hallows
J K Rowlin and Mary GrandPré
0545010225
20.99

Pride and Prejudice
Jane Austen
0307386864
7.95

Wuthering Heights
Emily Bronte
0553212583
4.95


using the following code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const int size = 50;
char Title[ size ];
char Author[ size ];
char ISBN[ size ];
float price;

// create a file object to read files
ifstream infile; 

// opens file
infile.open( "Books.txt" );

// get information from each line
infile.getline( Title, size );
infile.getline( Author, size );
infile.getline( ISBN, size );
infile >> price;

 // continue reading the file until the end
while ( !infile.fail())
{

// display   
cout << Title  << endl
     << Author << endl
     << ISBN   << endl
     << price  << endl << endl; 

// read another record
infile.getline( Title, size );
infile.getline( Author, size );
infile.getline( ISBN, size );
infile >> price;


}// end while

// close file
infile.close();


The problem is, the code is not reading the file and displaying the book info. Why? Any assistence will be greatly appreciated.
Last edited on
One problem is that you're attempting to read the price (float) into an int, and this
should fail.
jsmith,
Thanks, changed it. Stilll cant figure out why this code is not working.
Ok, I got it to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# include <iostream>
# include <fstream>

using namespace std;

int main()
{

const int size = 50;
char Title[ size ];
char Author[ size ];
char ISBN[ size ];
char price[ size ];
char line[ size ];


ofstream outfile;
outfile.open("myfilename.txt",ios::app);
outfile.close();

ifstream infile;
infile.open("myfilename.txt"); 
while ( infile ) 
{ 

   if (infile == false) 
   cout<<"Could not open the input file"<<endl;
   
   
   // read another record 
   infile.getline( Title, size ); 
   infile.getline( Author, size ); 
   infile.getline( ISBN, size ); 
   infile.getline( price, size ); 
   infile.getline( line, size );
   
   
      
   // display    
   cout << Title  << endl 
     << Author << endl 
     << ISBN   << endl 
     << price  << endl << endl; 

   


}// end while 

   
   infile.close();
   
   system ("pause");
}


1
2
3
4
5
6
7
8
9
10
11
Harry Potter and the Deathly Hallows 
J K Rowlin and Mary GrandPré 
0545010225 
20.99 
Pride and Prejudice 
Jane Austen 
0307386864 
7.95 
------------------------------------
Wuthering Heights 
Emily Bronte 
0553212583 
4.95
------------------------------------
Topic archived. No new replies allowed.