Reading data from a 2-column text file

Hello Guys,
I am quite new to C++. I have a data file (tab delimited text file) containing two columns of data like:

0.1588 0.0000
0.1640 0.0000
0.1692 0.0000
0.1740 0.0000
0.1778 0.0000
0.1805 0.0000
0.1818 0.0000
0.1814 0.0000

My intention is to read each line of the file, for example the first line will be

0.1588 0.0000

and then be able to pick each column data e.g. 0.1588 and 0.0000 for separate calculations. And then move to the next next line of the file, until the end of file.
I could get the lines, but I am stuck on how to get each column data for my calculation. Please, I need your suggestions.

Lool at std::string's find() and substr() functions.

But don't go anywhere near strtok() !

I assume you file reading code uses std::string?
Last edited on
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
...
while(!file.eof())
{
    double a, b;
    file >> a >> b; // extracts 2 floating point values seperated by whitespace

    // do something with them
}
I prefer the OP's idea. I generally read whole lines and then split the parameters.

The problem with the ostream extractors is that it's harder to add validation logic. (As I edit little config files - for unit tests - quite a bit, it keeps me sane if the test reports my dozy typos: ideally with line and column number).

Of course, it's important to understand the operator>> approach, too.
closed account (DSLq5Di1)
Aye, consistent formatting and input validation are very important with either approach.. but given the OP's input above, I dont see the need to fetch each line and split into tokens.
Thanks guys for your response.
I think I may not have made myself clear. When I open my text file containing the 2-column data, the result of each line comes as a string. Please take a look at the lines of code below.

[string Load;
ifstream openfile("I\\L-51.txt");
if (openfile.is_open())
{
while (!openfile.eof())
{
getline(openfile,Load);]

My problem is how I can possibly extract each column value of the line "Load", which is a string of elements (e.g. 0.1588 0.0000).
Thanks sloppy9. I am happy now :) Thanks you guys for your response. I really appreciate it.

Hello Guys, I tried going one step further by using the getline() function to simultaneously open a second file ( which is of the same format with the first file) which I also want to be included in the "while loop". Then, I get the following error:

error C2374: 'openfile': redefinition; multiple initialization

I understand what the error mean! Is there a way to get through this using by the same getline() ? I have tried using the apstring function for working with multiple files, but I keep getting the error that #include "apstring.h" cannot be opened. Is there a way to get around this?

Here is my code for reading the first file:

[
ifstream openfile("F:\\L-51.txt"); // Open load profile file
while (getline(openfile,Load))
{
istringstream is(Load);
double P;
double Q;
int n=0;
is >> P >> Q; // Reads P and Q separately from the file
double Pt, Qt;
double K=0.2101; // constant value
Pt = K*P;
Qt = K*Q;
cout << Pt << endl;


//cout << P << endl;



}
if (!openfile.eof())
{
cout << "Unable to open the file" << endl;
}
][/code]

Topic archived. No new replies allowed.