C++ input from file processing

Discussion in 'C++' started by Asif Zaidi, Feb 23, 2010.

  1. Asif Zaidi

    Asif Zaidi Guest

    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;
    }
     
    Asif Zaidi, Feb 23, 2010
    #1
    1. Advertising

  2. Asif Zaidi

    LR Guest

    Asif Zaidi wrote:
    > 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
     
    LR, Feb 23, 2010
    #2
    1. Advertising

  3. Asif Zaidi

    red floyd Guest

    On Feb 22, 6:27 pm, Asif Zaidi <> wrote:
    > 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<>.
     
    red floyd, Feb 23, 2010
    #3
  4. Asif Zaidi

    tonydee Guest

    On Feb 23, 11:27 am, Asif Zaidi <> wrote:
    > 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;
    }
    }
     
    tonydee, Feb 24, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maxim
    Replies:
    0
    Views:
    411
    Maxim
    Jul 7, 2003
  2. Gianni Mariani
    Replies:
    0
    Views:
    370
    Gianni Mariani
    Jan 6, 2004
  3. Arnold
    Replies:
    7
    Views:
    502
    Martijn Lievaart
    Jan 7, 2004
  4. Arnold
    Replies:
    9
    Views:
    436
    Martijn Lievaart
    Jan 7, 2004
  5. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    516
    Michael Foord
    Sep 17, 2004
Loading...

Share This Page