ifstream read error

closed account (zwA4jE8b)
Hey guys,
The following code reads in all the data from my input file correctly, and even outputs it correctly.
But at the end the infile.fail() message is displayed and my counter tells that it is on the last line. I know there is no newline character on the last line, but there is on every other line. Is there a way to make it not fail? What exactly is causing it to fail?

I have stepped through it and see that all the proper data is getting placed into all the proper fields.

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
void readin(lType *head, lType *tail)
{
	lType *newnode;
	ifstream infile;
	string holder;
	char dcard;
	int i = 3;

	infile.open("input.dat");

	getline(infile, holder);
	getline(infile, holder);
	if (infile.peek() == '\n')
			infile.get(dcard);

	while (!infile.eof() && !infile.fail())
	{
		i++;
		newnode = new lType;
		getline(infile, holder, ',');
		newnode->last = holder;
		infile.get(dcard);
		getline(infile, holder, ' ');
		newnode->first = holder;
		infile >> newnode->id;
		if (infile.peek() == '\n')
			infile.get(dcard);
		newnode->link = NULL;
		insert(head, tail, newnode);
	}

	if (infile.fail())
		cout << "File corrupted on line " << i << endl;

	infile.close();
}



This is my input file. it is a .dat ext.
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
        Name                 ID


Shepard, Alex                  228
Chase-Nason, Tyler             310
Wallendorf, Daniel             773
Kissner, Ross                  783
Snart, Carson                  322
Lambert, Thomas                164
Camel, Joe                     499
Brandt, Samantha               610
Stanger, Orion                 206
Harbert, Emily                 402
Wilson, Thayne                 267
Jensen, Paul                   577
Ervin, Michael                 521
Flowers, LeeAnn                991
Creekmore, Morgan              112
Terpstra, Dirk                 881
Sisneros, Eric                 491
Mangel, Randy                  949
Gates, Walter                  351
Blagg, Jesse                   835
Morris, Ryan                   277
Parker, Kyle                   404


Thank you.

p.s. Yes this was homework.either way I still need to understand why the fstream is failing.
Last edited on
closed account (zwA4jE8b)
I changed it too..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do
	{
		i++;
		newnode = new lType;

		getline(infile, holder, ',');
		newnode->last = holder;
		infile.get(dcard);

		getline(infile, holder, ' ');
		newnode->first = holder;

		infile >> newnode->id;

		newnode->link = NULL;
		insert(head, tail, newnode);
		
		if (infile.eof())
			break;
		if (infile.peek() == '\n')
			infile.get(dcard);
	}
	while (!infile.eof() && !infile.fail());



so it exits before it fails.

How do I get it to check for eof and not fail without using a break statement?
Last edited on
closed account (zwA4jE8b)
Ok it works,
Just chaned it to...

1
2
3
if (!infile.eof())
	if (infile.peek() == '\n')
	    infile.get(dcard);
Last edited on
Topic archived. No new replies allowed.