get integer values using ifstream error.

Discussion in 'C++' started by zhouchengly@gmail.com, Nov 7, 2006.

  1. Guest

    when I use the following code to get integer values from file:
    void getIntegers()
    {
    vector<ItemType > items;
    ifstream ifs("test.dat");
    //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£
    ItemType item ;
    while( !ifs.eof() )
    {
    ifs>>item;
    items.push_back(item);
    }
    }
    if test.dat is ended with a carriage return, "ifs>>item" will not fail
    but doesn't change item's value. this is not I want.
    using try...catch having no effect.

    So is there an stardard way to get integer(float) from a file? the file
    format is like this.
    //////test.dat
    101

    22

    35
    //end with a carriage return
     
    , Nov 7, 2006
    #1
    1. Advertising

  2. Markus Moll Guest

    Hi

    wrote:

    > while( !ifs.eof() )
    > {
    > ifs>>item;
    > items.push_back(item);
    > }


    This is an faq.
    Please go and read http://www.parashift.com/c -faq-lite/ before posting
    questions. Thanks.

    Markus
     
    Markus Moll, Nov 7, 2006
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    >when I use the following code to get integer values from file:
    >void getIntegers()
    >{
    > vector<ItemType > items;
    > ifstream ifs("test.dat");
    > //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£
    > ItemType item ;
    > while( !ifs.eof() )
    > {
    > ifs>>item;
    > items.push_back(item);
    > }
    >}
    >if test.dat is ended with a carriage return, "ifs>>item" will not fail
    >but doesn't change item's value. this is not I want.
    >using try...catch having no effect.
    >
    >So is there an stardard way to get integer(float) from a file?


    eof() cannot be set prior to testing whether the next
    item can be read or not. Also, failures other than eof
    are possible.
    And an exception will only be thrown if you configure the
    stream accordingly...

    Try the following loop:
    while( ifs>>item )
    items.push_back(item);

    Even easier, if your file contains a simple array of values,
    you can use istream_iterator ( #include <iterator> ):

    typedef std::istream_iterator<ItemType> InItem;
    std::vector<ItemType> const items( (InItem(ifs)), InItem() );
    // directly initializes the collection from the file
    Note
    - the collection can then (optionally) be 'const', which
    is a good idea for safety if you don't edit the vector.
    - the extra parenthesis around the first parameter is somehow
    required for syntactic disambiguation with a function
    declaration ( see "C++ most vexing parse" or a FAQ...)

    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Nov 7, 2006
    #3
  4. Guest

    thanks!
    an extra question:
    how to read the first n elements of the file using the second solution?
    "
    typedef std::istream_iterator<ItemType> InItem;
    std::vector<ItemType> const items( (InItem(ifs)), InItem() );
    "

    "Ivan Vecerina дµÀ£º
    "
    > <> wrote in message
    > news:...
    > >when I use the following code to get integer values from file:
    > >void getIntegers()
    > >{
    > > vector<ItemType > items;
    > > ifstream ifs("test.dat");
    > > //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£
    > > ItemType item ;
    > > while( !ifs.eof() )
    > > {
    > > ifs>>item;
    > > items.push_back(item);
    > > }
    > >}
    > >if test.dat is ended with a carriage return, "ifs>>item" will not fail
    > >but doesn't change item's value. this is not I want.
    > >using try...catch having no effect.
    > >
    > >So is there an stardard way to get integer(float) from a file?

    >
    > eof() cannot be set prior to testing whether the next
    > item can be read or not. Also, failures other than eof
    > are possible.
    > And an exception will only be thrown if you configure the
    > stream accordingly...
    >
    > Try the following loop:
    > while( ifs>>item )
    > items.push_back(item);
    >
    > Even easier, if your file contains a simple array of values,
    > you can use istream_iterator ( #include <iterator> ):
    >
    > typedef std::istream_iterator<ItemType> InItem;
    > std::vector<ItemType> const items( (InItem(ifs)), InItem() );
    > // directly initializes the collection from the file
    > Note
    > - the collection can then (optionally) be 'const', which
    > is a good idea for safety if you don't edit the vector.
    > - the extra parenthesis around the first parameter is somehow
    > required for syntactic disambiguation with a function
    > declaration ( see "C++ most vexing parse" or a FAQ...)
    >
    > hth -Ivan
    > --
    > http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    > Brainbench MVP for C++ <> http://www.brainbench.com
     
    , Nov 7, 2006
    #4
  5. emil Guest

    wrote:
    > when I use the following code to get integer values from file:
    > void getIntegers()
    > {
    > vector<ItemType > items;
    > ifstream ifs("test.dat");
    > //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£
    > ItemType item ;
    > while( !ifs.eof() )
    > {
    > ifs>>item;
    > items.push_back(item);
    > }
    > }
    > if test.dat is ended with a carriage return, "ifs>>item" will not fail
    > but doesn't change item's value. this is not I want.
    > using try...catch having no effect.
    >
    > So is there an stardard way to get integer(float) from a file? the file
    > format is like this.
    > //////test.dat
    > 101
    >
    > 22
    >
    > 35
    > //end with a carriage return



    you can use fscanf func
     
    emil, Nov 7, 2006
    #5
  6. Guest

    It's a solution, but considering the efficiency, I prefer to use stream
    than fscanf.


    "emil дµÀ£º
    "
    > wrote:
    > > when I use the following code to get integer values from file:
    > > void getIntegers()
    > > {
    > > vector<ItemType > items;
    > > ifstream ifs("test.dat");
    > > //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£
    > > ItemType item ;
    > > while( !ifs.eof() )
    > > {
    > > ifs>>item;
    > > items.push_back(item);
    > > }
    > > }
    > > if test.dat is ended with a carriage return, "ifs>>item" will not fail
    > > but doesn't change item's value. this is not I want.
    > > using try...catch having no effect.
    > >
    > > So is there an stardard way to get integer(float) from a file? the file
    > > format is like this.
    > > //////test.dat
    > > 101
    > >
    > > 22
    > >
    > > 35
    > > //end with a carriage return

    >
    >
    > you can use fscanf func
     
    , Nov 7, 2006
    #6
  7. Markus Moll Guest

    Hi

    wrote:

    > It's a solution, but considering the efficiency, I prefer to use stream
    > than fscanf.
    >
    >
    > "emil 写é“:

    [...]
    >> you can use fscanf func


    Again, _please_ read the FAQ ( http://www.parashift.com/c -faq-lite/ )
    before posting. In this case especially [5.4], where it says: "do not
    top-post". Thanks.

    Markus
     
    Markus Moll, Nov 7, 2006
    #7
  8. <> wrote in message
    news:...
    >thanks!

    NB: please don't top-post, see
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.4

    >an extra question:
    >how to read the first n elements of the file using the second solution?
    >"
    >typedef std::istream_iterator<ItemType> InItem;
    >std::vector<ItemType> const items( (InItem(ifs)), InItem() );
    >"


    Sadly, I can't think of an obvious way to read a fixed number
    of elements using the above approach - not using the standard
    library alone.
    Regrettably, I think in this case I would revert to using a loop:
    std::vector<ItemType> items(n);
    for( unsigned i = 0 ; i<n ; ++i )
    ifs >> items;

    Various C++ library implementations provide extensions that would also
    help with this, for example: http://www.sgi.com/tech/stl/copy_n.html
    std::vector<ItemType> items;
    copy_n( istream_iterator<ItemType>(ifs), n, back_inserter(items) );

    It would also be possible to write an iterator wrapper to allow
    writing:
    typedef count_iterator<std::istream_iterator<ItemType> > InItem;
    std::vector<ItemType> const items( (InItem(ifs)), InItem(n) );



    I don't know if the planned/official C++ library extensions (TR1, TR2,
    or C++0x) include a facility that would support any of the above...

    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Nov 7, 2006
    #8
    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. cylin
    Replies:
    7
    Views:
    3,967
    Kevin Goodsell
    Sep 19, 2003
  2. yingmu

    ifstream reading integer problem

    yingmu, Nov 13, 2006, in forum: C Programming
    Replies:
    1
    Views:
    7,739
    PraveenAyinikkal
    Nov 14, 2006
  3. James Lehman
    Replies:
    1
    Views:
    526
    Pavel
    Apr 23, 2006
  4. Randy Kramer
    Replies:
    12
    Views:
    403
    Robert Klemme
    Oct 25, 2007
  5. GIMME
    Replies:
    1
    Views:
    193
    Dr John Stockton
    Apr 9, 2004
Loading...

Share This Page