C++ input from file processing

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;
}
 
L

LR

Asif said:
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:



int main ()
{
string line;
ifstream myfile ("d:/output2.txt");

if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);


Just off the top of my head...


std::getline reads a line at a time and stored the character.
If you want to manipulate what you've read into line as ints then
you might want to consider something like this.

Alternatively you could just read the data from the file as ints and
chars, but it might be a little more difficult.

std::string line;
while(std::getline(myfile,line)) {
int year;
char colon;
std::istringstream in(line);
if(in >> year >> colon) {
// check for validity of data and
// do something...
int n;
while(in >> n) {
// do something...
}
}
else {
// we didn't find a year and a colon
}
}


cout << line << endl;
cout << line[0] << endl;

// process numbers here
}

myfile will close by itself when its dtor is called.
myfile.close();
}

else cout << "Unable to open file" << endl;
}

LR
 
R

red floyd

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() )
This loop does not do what you think. FAQ 15.5
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
                  {
                          getline (myfile,line);
                          cout << line << endl;
                          cout << line[0] << endl;

                          // process numbers here
                  }
                  myfile.close();
          }

          else cout << "Unable to open file" << endl;

}

Create an istringstream from your input line and parse the line.
operator[] on a std::string returns a reference to charT -- usually
either char or wchar_t, depending on if it's a string or a wstring
(or however you've decided to specialize basic_string<>.
 
T

tonydee

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;

}

Similar to LR's response, but saving the values into a vector...

Cheers,
Tony

int previous_year = -1;
int year;
char colon;
std::vector<int> values;

std::string line;

while (std::getline(the_stream, line))
{
std::istringstream iss(line);
if (not iss >> year >> colon or colon != ':')
{
std::cerr << "invalid data format '" << line << "'\n';
break;
}
if (year == previous_year)
process(year, values);
else
{
values.clear();
int n;
while (iss >> n)
values.push_back(n);
process(year, values);
previous_year = year;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top