fstream

closed account (DEUX92yv)
I'm trying to read in various values and the fstream object is giving me garbage values, but for only certain 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
	ifstream readfriends;
	readfriends.open(fileIn);
	if (! readfriends.is_open()) {						// Error opening
		return -1;
	}

	string tmpname, tmpfriends, tmp;
	int initusers, tmpid, tmpyear, tmpzip, tmpfriend;

	readfriends >> initusers;
	while (readfriends.good()) {							// Read in components
		readfriends >> tmpid;
		getline(readfriends, tmpname);
		readfriends >> tmpyear;
		readfriends >> tmpzip;
		add_user(tmpname, tmpyear, tmpzip);	// Add user to database
		getline(readfriends, tmpfriends);			// Get friend list
		for (unsigned int i = 0; i < tmpfriends.length(); i += 2) {
			tmp = tmpfriends[i];
			stringstream confriend(tmp);
			confriend >> tmpfriend;
			Users[tmpid].add_friend(tmpfriend);
		}
		cout << endl;
	}
	readfriends.close();


Output (cout each field that I read in):
initusers: 5
tmpid: 0
tmpname: // Blank 
tmpyear: 0 // Wrong
tmpzip: 16777220 // Very wrong
tmpfriends: // Blank


It only cycles through the while loop once, though it should iterate five times (in the case of this file).
The text file I'm reading from:
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
5
0
	First1 Last1
	1978
	90018
	1 2 3
1
	First2 Last2
	1979
	90201
	0 2
2
	First3 Last3
	1885
	90089
	0 1
3
	First4 Last4
	1945
	91103
	0 4
4
	First5 Last5
	1988
	90007
	3

Does the fstream object not ignore tab characters?
Last edited on
Does the fstream object not ignore tab characters?

By default, it does. But getline() doesn't.
But are you sure the problem isn't from add_user()?
closed account (DEUX92yv)
That's what I thought about fstream objects and getline(). I don't think add_user() is the problem; or perhaps I simply don't know how it could be. When I put cout << statements right below every readfriends >> instance it produced the output above. If add_user() was the problem, wouldn't only the variables used in it be affected? I don't see how that could change tmpname, for instance.
Last edited on
The problem is that when you read the tmpid readfriends >> tmpid; only the number is read from the file and the next read operation will start on the character after it, which is a new line character. When you read the tmpname getline(readfriends, tmpname); it finds a new line character right away so it stops reading and tmpname becomes an empty string.

You will have to discard the new line character before calling getline. This is often done by using the ignore member function but since you want to ignore the tab as well I think it's better to use std::ws.
1
2
3
4
5
...
readfriends >> tmpid;
readfriends >> std::ws;
getline(readfriends, tmpname);
...
Last edited on
Copy-paste your add_user() and add_friend() functions.
I can't see any problems with your current code, except poor style.


Peter's probably right.
Last edited on
Topic archived. No new replies allowed.