Getting numbers from each line of a file
Mar 25, 2016 at 1:38pm UTC
I'm trying to get the numbers from every line of a file, each line has the same format:
where A, B, C, D are the numbers that I want.
1 2 3 4 5 6 7
ifstream database("path/file.txt" );
string line;
if (database.is_open()){
while (getline(database, line)){
}
}
database.close();
If I do it like this then what should I do with the line to separate all the number?
Mar 25, 2016 at 2:03pm UTC
Since you have the line contained in a string you can use a stringstream to parse that string.
1 2 3 4
while (getline(database, line)){
stringstream sin(line);
sin >> number1 >> number2 >> number3 >> number4;
}
Mar 25, 2016 at 2:19pm UTC
Hi, jlb. I tried your suggestion with the following test code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
ifstream database("file.txt" );
string line;
int A, B, C, D;
if (database.is_open()){
while (getline(database, line)){
stringstream sin(line);
sin >> A >> B >> C >> D;
}
}
cout<<"A: " <<A<<endl<<"B: " <<B<<endl<<"C: " <<C<<endl<<"D: " <<D<<endl;
}
Well, after I compile, there is an error:
[Error] variable 'std::stringstream sin' has initializer but incomplete type
If I get anything wrong then would you please tell me, thank you in advance!
Mar 25, 2016 at 2:32pm UTC
[Error] variable 'std::stringstream sin' has initializer but incomplete type
You need to include the stringstream header
Mar 25, 2016 at 2:33pm UTC
Since you have this error did you find and study the documentation for the stringstream class? If you would've you should've seen that this class is defined in a header file. Did you try to include that required header file?
Mar 25, 2016 at 3:01pm UTC
Oops... I thought it was in the string header... Sorry for my stupidness...
But yet again, I have another problem now... The program has run...
Here is my "file.txt":
512\t604\t383\t565
898\t6558\t485\t1212
6541\t252\t4562\t156
And the output I get:
A: 6541
B: 0
C: 1992937936
D: 2752460
But the output I desire is:
A: 6541
B: 252
C: 4562
D: 156
Is "\t" a problem?
Mar 25, 2016 at 3:17pm UTC
Yes. Inside a C++ program , use '\t' to represent a tab. But in the actual text file, just press the tab key to type a real tab.
file.txt
512 604 383 565
898 6558 485 1212
6541 252 4562 156
Mar 25, 2016 at 3:24pm UTC
When I run the program I get your desired output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include<iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
int main(){
//ifstream database("file.txt");
stringstream database("512\t604\t383\t565\n898\t6558\t485\t1212\n6541\t252\t4562\t156\n" );
string line;
int A, B, C, D;
while (getline(database, line)){
stringstream sin(line);
sin >> A >> B >> C >> D;
}
cout<<"A: " <<A<<endl<<"B: " <<B<<endl<<"C: " <<C<<endl<<"D: " <<D<<endl;
}
Output:
1 2 3 4
A: 6541
B: 252
C: 4562
D: 156
You may want to move your cout statement into the loop to possibly see where your problem begins.
Mar 25, 2016 at 3:35pm UTC
Problem solved. I replace "\t" in my text file with tab and it went well. Thank you for the help!!
Topic archived. No new replies allowed.