A
Asif Zaidi
Hi:
I am trying to process data from a file. The problem I am having is
the data is recognized only as char. I will explain below
Data Set: (below is my data set. Assume it is in file output2.txt)
1851: 80 70 50 40
1851: 40 60 80 90 70 60 80 100 90 40 40 40
1852: 70 60
1852: 70 70 70 70 60
What I want:
-------------------
I read this input file and I get each line.
The integers are processed in each line
Read next line. If yr is the same as previous line, process with data
from last line else treat as new data
Where I am stuck
--------------------------
When I read each line, I cannot get the data. I am reading into var
'line'. But I thought line[0] was 1851 but in fact it is 1 and
line[1]=8 etc... I am not able to get past this step let alone the
rest of the code !!
My code so-far
-----------------------
int main ()
{
string line;
ifstream myfile ("d:/output2.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
cout << line[0] << endl;
// process numbers here
}
myfile.close();
}
else cout << "Unable to open file" << endl;
}
I am trying to process data from a file. The problem I am having is
the data is recognized only as char. I will explain below
Data Set: (below is my data set. Assume it is in file output2.txt)
1851: 80 70 50 40
1851: 40 60 80 90 70 60 80 100 90 40 40 40
1852: 70 60
1852: 70 70 70 70 60
What I want:
-------------------
I read this input file and I get each line.
The integers are processed in each line
Read next line. If yr is the same as previous line, process with data
from last line else treat as new data
Where I am stuck
--------------------------
When I read each line, I cannot get the data. I am reading into var
'line'. But I thought line[0] was 1851 but in fact it is 1 and
line[1]=8 etc... I am not able to get past this step let alone the
rest of the code !!
My code so-far
-----------------------
int main ()
{
string line;
ifstream myfile ("d:/output2.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
cout << line[0] << endl;
// process numbers here
}
myfile.close();
}
else cout << "Unable to open file" << endl;
}