Error in reading text file

Im trying to read a set a data from text file in a structure array.
However my code works fine with all the other data pass the first one where it reading 0001 instead of 10001

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
55
56
57
58
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>
#include<cstring>

using namespace std; 

typedef struct
{
	char num[6];
	char des[20];
	double price;
}PRODUCT;

void list(PRODUCT p[], int size);

int main(void)
{
	PRODUCT product[10];
	ifstream inFile;
	int count = 0;
	inFile.open("B4.txt");
	if (inFile.fail())
	{
		cout << "Error opening file.\n";
	}
	else
	{
		for (int i = 0; !inFile.eof(); i++)
		{
			inFile.ignore();
			inFile.getline(product[i].num, 6);
			inFile.getline(product[i].des, 31);
			inFile >> product[i].price;
			count++;
		}
		inFile.close();
	}
	list(product, count);
}

void list(PRODUCT p[], int size)
{
	cout << fixed <<left;
	cout << setw(10) << "CODE";
	cout << setw(30) << "DESCRIPTION";
	cout << setw(10) << "PRICE";
	cout << "\n";

	for (int i = 0; i < size; i++)
	{
		cout << setw(10) << p[i].num;
		cout << setw(30) << p[i].des;
		cout << setw(10) << p[i].price;
		cout << "\n";
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
10001
Product 1
20.3
10002
Product 2
30.3
10003
Product 3
50
10004
Product 4
69.69
Last edited on
Because your ignore is at the start of the loop, not the end.
1
2
3
4
5
6
7
	for (int i = 0; i < 10 && inFile.getline(product[i].num, 6) ; i++)
		{
			inFile.getline(product[i].des, 31);
			inFile >> product[i].price;
			inFile.ignore();
			count++;
		}

1. Always include a loop limiter if you're reading into an array.

2. eof() is a tricky thing to use right (you weren't).
For example, if your file has exactly one line, and you read that line using getline(), then eof() is still false.
eof() only becomes true when some prior getline/get/>> operation has failed.

i dont quite understand the condition of the for loop. Why do i need to add another condition and not just i < 10 ? And why we do not need to read any more product[i].num within the for loop ? Does it get read automatically each time when it is checking the condition ?
> Does it get read automatically each time when it is checking the condition ?
Yes.

Well it's read only if the i<10 part is true.
If i<10 is false (and your array is full), then the right hand side of the && is not evaluated at all.

Otherwise, you end up with something messier like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int i = 0; i < 10 ; i++)
{
	if ( inFile.getline(product[i].num, 6) )
	{
		// pedantically, every input should be checked for success
		inFile.getline(product[i].des, 31);
		inFile >> product[i].price;
		inFile.ignore();
		count++;
	}
	else
	{
		// no more data
		break;
	}
}

Now i did try do something as u did there without checking where my for loop only has a condition of i < 10. If somehow the data from the file is inusufficient then is it the reason on why the restover of the array is filled with some random number and data ?
Well your struct lacks a constructor, and your array declaration lacks an initialiser, so it will contain random data to begin with.

But that's why you also maintain count to tell you how much of the array is real information.
Instead of .ignore(), you can use >> ws to skip all following white-space chars.

1
2
3
4
5
for (count = 0; count < 10 && inFile.getline(product[count].num, 6); ++count) {
    // pedantically, every input should be checked for success
    inFile.getline(product[count].des, 31);
    inFile >> product[count].price >> ws;
}

Topic archived. No new replies allowed.