Help reading .txt file.

So I am relatively new to c++ and have read a lot of different documentation on reading information from a text file but for some reason I can't get this to work right.

Lets assume my text file is something like this:

name1,name5,45,67,41134
name1,name3,60,12,41141
name5,name2,70,55,41158
name5,name1,33,80,41161
name3,name5,70,94,41175
name5,name1,47,34,41183

My end goal is to search the file and anytime name5 is at the start of a new line then I need to read that information into an array of some sort so I can use the data in a lot of different ways.

The code below is one attempt at just trying to count how many times that name5 shows up in the document (this is needed as well but not as important as the above information). The issue with the code is that when it is ran it says that name5 shows up 2 times instead of 5 which means it only counts it correctly if name5 is in the middle of the line and not at the start.

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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(){

	string name;
	string vip = "name5";
	int counter = 0;

	ifstream namesLog ( "file.txt" );

	if ( namesLog.is_open() ){

		while( namesLog.good() ){
			getline (namesLog, name, ',');

			if ( vip == name){
				counter++;
			}	
		}
		namesLog.close();
	}
	else {
		cout << endl << "The file did not open." << endl;
	}
	cout << endl << "The VIP appeared in the file " << counter << " times." << endl;
}


Any help as to why it only reads name5 when it is in the middle of the line or what I can do to achieve the main goal (anytime name5 starts a new line store the information in an array or multiple arrays) would be great.
Your delimiter ',' isn't at the end of the line. So, my guess would be that, when you're on line 2 of your input file, you're reading "41141\nname5"

Note, that '\n' is the carriage return( new line ).

I haven't run it, but it seems like it could be the problem.


Edit:
So, I stopped being lazy and ran it. Turns out it was reading "41141\nname5".

One way around this is to collect the line each iteration of the loop. i.e.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	string name1, name2, num1, num2, num3;
	string vip = "name5";
	int counter = 0;

	ifstream namesLog ( "file.txt" );

	if ( namesLog.is_open() ){

		while( namesLog.good() ){
			getline (namesLog, name1, ',');
			getline (namesLog, name2, ',');
			getline (namesLog, num1, ',');
			getline (namesLog, num2, ',');
			getline (namesLog, num3, '\n');

			if( ( vip == name1 ) || ( vip == name2 ) )
				counter++;
		}
		namesLog.close();
	}
	else {
		cout << endl << "The file did not open." << endl;
	}
	cout << endl << "The VIP appeared in the file " << counter << " times." << endl;
Last edited on
Well my understanding was that by default the delimiter is \n so if I do a getline using the default it will return the entire line "name1,name3,60,12,41141" in to my string. Also if I add the code:

 
cout << endl << name << endl; 


after line 18 it will display "name5" and not "41141\nname5". So then when it hits line 20 it should be comparing it to the correct thing but it will not enter the loop.

EDIT: I just realized it was "41141\nname5" but when it displayed it was showing it as

41141
name5

So I thought it was reading it correctly. Woops.
Last edited on
I edited my post above. But when you cout the name, it would show:
41141
name5


Because it's still using the \n to go down a line. The '\n' from the middle of the string.


Run the following, it may help you understand what I mean:
1
2
3
	string name = "hi\nthere ho\nw are you\n?";

	cout << name << '\n';
Last edited on
Well they way you set it up fixed that issue and another one, awesome. Thanks a lot!
lol np. And if you're going to do anything with the numbers, i.e. Addition etc. you may want to look in to stringstream.
Topic archived. No new replies allowed.