Program storing garbage...

So I'm writing a program (in Ubuntu, if that means anything) to test out file i/o. However, for some reason, my program stores garbage data that I don't know about. Is there any way to prevent this? I've tried multiple tests to rectify the issue but it still comes up. I have my source and text file attached for view.

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
59
/*
  this program tests some get line stuff for sadistic fun.
*/
#include <iostream>
#include <fstream>
using namespace std;

const int SIZE_LIMIT = 20;
const int NAME_SIZE = 20;
const int AGE_SIZE = 2;
const int WEIGHT_SIZE = 5;

struct Person
{
	char fullName[NAME_SIZE];
	int age;
	char hobby[NAME_SIZE];
	double weight;
};

typedef Person DatingSite[NAME_SIZE];

void LoadFile(fstream&, int&, DatingSite);

int main()
{
	int count = 0;

	DatingSite Bachelor;	
	fstream testFile;

	LoadFile(testFile, count, Bachelor);

	return 0;
}

void LoadFile(fstream &File, int &count, DatingSite Bachelor)
{
	File.open("test.txt", ios::in);
	do
	{
		if (File.eof() || File.fail() || File.bad())
			break;

		File.getline(Bachelor[count].fullName, NAME_SIZE, ',');
		File >> Bachelor[count].age, AGE_SIZE; 
		File.ignore(1);
		File.getline(Bachelor[count].hobby, NAME_SIZE, ','); 
		File >> Bachelor[count].weight;
		File.ignore(1);
		cout << Bachelor[count].fullName << endl;
		cout << Bachelor[count].age << endl;
		cout << Bachelor[count].hobby << endl;
		cout << Bachelor[count].weight << endl;
		cout << "The count is " << count << endl;
		count++;
	} while(!File.eof());

}


Here is the text file that I am reading from

test.txt
1
2
3
4
5
Josh Johnson,20,pr0n,210.12
Elizabeth Saders,26,Soaps,307.28
Samir Umar,32,Bomb Making,155.52
Bob Marley,30,Smoking Bud,127.12
Thora Birch,16,Acting,118.09


And finally, here is my output

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
Josh Johnson
20
pr0n
210.12
The count is 0
Elizabeth Saders
26
Soaps
307.28
The count is 1
Samir Umar
32
Bomb Making
155.52
The count is 2
Bob Marley
30
Smoking Bud
127.12
The count is 3
Thora Birch
16
Acting
118.09
The count is 4

64

2.77981e-310
The count is 5


Any help would be greatly appreciated :-D.
Last edited on
You're trying to read beyond the end of the file. The eof flag doesn't get set until you try to read beyond the end of the file.
Is there a test condition that I can set to prevent this (other than setting a numerical limit), or will my program be subject to this at all times?
The prob is you calling multiple i/p operations (getline() and >>) between your checks of eof()
Andy: If that's the case, the reason I'm using multiple operators is because I don't know of another method to store integers (or any other non-character based numbers) in my variables other than the extraction operator. I would have like to used to getline but I ran into problems along the way. :-(

Appreciate all the feedback so far, I'm really learning a lot through the responses. ^_^
This stops the file at the right place, if the final line in complete (extra eof() check after first getline)

But is NOT a nice solution!

Do you know how to split a strings up? And convert a string to a number?

Or are you familiar with stringstream??

Andy

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
void LoadFile(fstream &File, int &count, DatingSite Bachelor)
{
	File.open("test_0.txt", ios::in);
	do
	{
		if (File.eof() || File.fail() || File.bad())
			break;

		File.getline(Bachelor[count].fullName, NAME_SIZE, ',');
		if(File.eof())
			break;
		File >> Bachelor[count].age, AGE_SIZE; 
		File.ignore(1);
		File.getline(Bachelor[count].hobby, NAME_SIZE, ','); 
		File >> Bachelor[count].weight;
		File.ignore();
		cout << Bachelor[count].fullName << endl;
		cout << Bachelor[count].age << endl;
		cout << Bachelor[count].hobby << endl;
		cout << Bachelor[count].weight << endl;
		cout << "The count is " << count << endl;
		count++;
	} while(!File.eof());

}


Last edited on
Andy: Thanks again for the help.

To answer your questions:
I don't know how to split strings, only know how to concatenate for now.
I do know how to convert a character array to numbers using atoi, atol and atof.
I have yet to use stringstream.
I got it to work using atoi and atof. Thanks for the suggestion Andy. This thread may be closed now.
Topic archived. No new replies allowed.