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.
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.
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).
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:
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]