File I/O and conversion

Discussion in 'C++' started by Evyn, Sep 27, 2007.

  1. Evyn

    Evyn Guest

    HI all,

    I have a text file of integers. I can format the file so that the
    integers are 1 per line, or continuous.

    Reading the file is not a problem. What I want to do is sum the
    integers and get a mean. Easy enough, but how do I convert the
    character or string I read into type int? I have tried with getline, 1
    integer per line in my input file, and get with the integers in 1
    line. No luck.

    First attempt
    -------------------

    string line;
    ifstream myfile (f);
    if (myfile.is_open())
    {
    while (! myfile.eof() )
    {
    getline (myfile,line);
    // sum = sum + (int) line; // Does not work
    running++;
    }
    myfile.close();
    }
    else cout << "Unable to open file";
    mean = sum/running;
    cout << "Mean = " << mean << " sum " << sum << " Running " << running
    << endl << endl;;


    second attempt
    ----------------------

    ifstream f1(f);
    if(!f1)
    {
    cout << "Error opening file" << endl;
    exit(1);
    }
    while(f1.get(ch) != NULL)
    {
    // int tmp = (int) ch; // Not working
    sum = sum + tmp;
    cout << "ch " << ch << " sum " << sum << endl;
    running++;
    }

    mean = sum/running;
    cout << "Mean = " << mean << " sum " << sum << " Running " <<
    running << endl;


    Any advice/pointers much appreciated.

    Regards,
    Jim
    Evyn, Sep 27, 2007
    #1
    1. Advertising

  2. Evyn

    Tim Love Guest

    Evyn <> writes:

    >how do I convert the
    >character or string I read into type int?

    I've not read your code in great detail, but

    * To convert a string to a number, read the FAQ
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.2

    * If you read characters from the file, the odds are that you're
    reading ASCII characters - i.e. a 49 in the file would represent '1',
    etc.
    Tim Love, Sep 27, 2007
    #2
    1. Advertising

  3. Have a look at the following functions:
    atoi()
    cin

    There's also a lexical parser in boost which does this but is probably
    using a sledgehammer to crack to a peanut in this case.

    Jonathan.

    --

    On Sep 27, 12:42 pm, Evyn <> wrote:
    > HI all,
    >
    > I have a text file of integers. I can format the file so that the
    > integers are 1 per line, or continuous.
    >
    > Reading the file is not a problem. What I want to do is sum the
    > integers and get a mean. Easy enough, but how do I convert the
    > character or string I read into type int? I have tried with getline, 1
    > integer per line in my input file, and get with the integers in 1
    > line. No luck.
    >
    > First attempt
    > -------------------
    >
    > string line;
    > ifstream myfile (f);
    > if (myfile.is_open())
    > {
    > while (! myfile.eof() )
    > {
    > getline (myfile,line);
    > // sum = sum + (int) line; // Does not work
    > running++;
    > }
    > myfile.close();
    > }
    > else cout << "Unable to open file";
    > mean = sum/running;
    > cout << "Mean = " << mean << " sum " << sum << " Running " << running
    > << endl << endl;;
    >
    > second attempt
    > ----------------------
    >
    > ifstream f1(f);
    > if(!f1)
    > {
    > cout << "Error opening file" << endl;
    > exit(1);
    > }
    > while(f1.get(ch) != NULL)
    > {
    > // int tmp = (int) ch; // Not working
    > sum = sum + tmp;
    > cout << "ch " << ch << " sum " << sum << endl;
    > running++;
    > }
    >
    > mean = sum/running;
    > cout << "Mean = " << mean << " sum " << sum << " Running " <<
    > running << endl;
    >
    > Any advice/pointers much appreciated.
    >
    > Regards,
    > Jim
    Jonathan Lane, Sep 27, 2007
    #3
  4. Evyn

    Evyn Guest

    Evyn, Sep 27, 2007
    #4
  5. Evyn

    Evyn Guest

    On Sep 27, 1:50 pm, Jonathan Lane <>
    wrote:
    > Have a look at the following functions:
    > atoi()
    > cin


    Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
    \irrational.cpp invalid conversion from `char' to `const char*'
    Evyn, Sep 27, 2007
    #5
  6. Evyn

    Jim Langston Guest

    "Evyn" <> wrote in message
    news:...
    > HI all,
    >
    > I have a text file of integers. I can format the file so that the
    > integers are 1 per line, or continuous.
    >
    > Reading the file is not a problem. What I want to do is sum the
    > integers and get a mean. Easy enough, but how do I convert the
    > character or string I read into type int? I have tried with getline, 1
    > integer per line in my input file, and get with the integers in 1
    > line. No luck.
    >
    > First attempt
    > -------------------
    >
    > string line;
    > ifstream myfile (f);
    > if (myfile.is_open())
    > {
    > while (! myfile.eof() )
    > {
    > getline (myfile,line);
    > // sum = sum + (int) line; // Does not work
    > running++;
    > }
    > myfile.close();
    > }
    > else cout << "Unable to open file";
    > mean = sum/running;
    > cout << "Mean = " << mean << " sum " << sum << " Running " << running
    > << endl << endl;;
    >
    >
    > second attempt
    > ----------------------
    >
    > ifstream f1(f);
    > if(!f1)
    > {
    > cout << "Error opening file" << endl;
    > exit(1);
    > }
    > while(f1.get(ch) != NULL)
    > {
    > // int tmp = (int) ch; // Not working
    > sum = sum + tmp;
    > cout << "ch " << ch << " sum " << sum << endl;
    > running++;
    > }
    >
    > mean = sum/running;
    > cout << "Mean = " << mean << " sum " << sum << " Running " <<
    > running << endl;


    1. Read the file data as an ints in the first place, not a string.
    2. Use stringstream to convert string to int (C++ way)
    3. Use atoi to convert c-style string to int (C way)

    #include <sstream>
    #include <iostream>
    #include <fstream>
    #include <string>

    int main()
    {
    std::ifstream Foo("Numbers.txt");
    int Value;
    while ( Foo >> Value )
    {
    std::cout << Value << "\n";
    }
    Foo.close();

    std::ifstream Foo2("Numbers.txt");
    std::string Line;
    while ( std::getline( Foo2, Line ) )
    {
    std::stringstream Bar( Line );
    Bar >> Value;
    std::cout << Value << "\n";
    }
    Foo2.close();

    std::ifstream Foo3("Numbers.txt");
    while ( std::getline( Foo3, Line ) )
    {
    Value = atoi( Line.c_str() );
    std::cout << Value << "\n";
    }

    }
    Jim Langston, Sep 27, 2007
    #6
  7. Evyn

    Evyn Guest

    > 2. Use stringstream to convert string to int (C++ way)

    Thank you - this method seems to work very well.
    Evyn, Sep 27, 2007
    #7
  8. Evyn

    Tim Slattery Guest

    Evyn <> wrote:

    >On Sep 27, 1:50 pm, Jonathan Lane <>
    >wrote:
    >> Have a look at the following functions:
    >> atoi()
    >> cin

    >
    >Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
    >\irrational.cpp invalid conversion from `char' to `const char*'


    atoi wants a C-style string as an argument. It sounds to me like
    you're passing it a char instead. This won't work:

    char x;
    int xi;
    x = '1'
    xi = atoi(x);

    But this will:

    char x[5];
    int xi;
    strcpy (x, "1");
    xi = atoi(x);

    There's a short discussion of atoi here:
    http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

    --
    Tim Slattery

    http://members.cox.net/slatteryt
    Tim Slattery, Sep 27, 2007
    #8
  9. Evyn

    Evyn Guest

    On Sep 27, 3:00 pm, Tim Slattery <> wrote:
    > Evyn <> wrote:
    > >On Sep 27, 1:50 pm, Jonathan Lane <>
    > >wrote:
    > >> Have a look at the following functions:
    > >> atoi()
    > >> cin

    >
    > >Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
    > >\irrational.cpp invalid conversion from `char' to `const char*'

    >
    > atoi wants a C-style string as an argument. It sounds to me like
    > you're passing it a char instead. This won't work:
    >
    > char x;
    > int xi;
    > x = '1'
    > xi = atoi(x);
    >
    > But this will:
    >
    > char x[5];
    > int xi;
    > strcpy (x, "1");
    > xi = atoi(x);
    >
    > There's a short discussion of atoi here:http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html
    >
    > --
    > Tim Slattery
    > ://members.cox.net/slatteryt


    Thanks!
    Evyn, Sep 27, 2007
    #9
  10. Evyn

    Guest

    On 27 sep, 07:42, Evyn <> wrote:
    > HI all,
    >
    > I have a text file of integers. I can format the file so that the
    > integers are 1 per line, or continuous.
    >
    > Reading the file is not a problem. What I want to do is sum the
    > integers and get a mean. Easy enough, but how do I convert the
    > character or string I read into type int? I have tried with getline, 1
    > integer per line in my input file, and get with the integers in 1
    > line. No luck.


    C++ iostreams have formatting capabilities, so you don't have to first
    read a string from the file, and then convert it to a number. You can
    read a number directly from the stream:

    std::ifstream f("input.txt");
    int num;

    while (f >> num){
    // Do something with num
    }
    , Sep 27, 2007
    #10
  11. Evyn

    Default User Guest

    Re: File I/O and conversion - TPA

    Jonathan Lane wrote:

    > Have a look at the following functions:


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Sep 27, 2007
    #11
    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. Victor Hannak
    Replies:
    0
    Views:
    7,427
    Victor Hannak
    Feb 20, 2004
  2. John Redmond
    Replies:
    0
    Views:
    604
    John Redmond
    Mar 26, 2009
  3. , India
    Replies:
    2
    Views:
    448
    Fraser Ross
    Sep 15, 2009
  4. Henry Lenzi
    Replies:
    6
    Views:
    123
Loading...

Share This Page