Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get integer values using ifstream error.

Reply
Thread Tools

get integer values using ifstream error.

 
 
zhouchengly@gmail.com
Guest
Posts: n/a
 
      11-07-2006
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

 
Reply With Quote
 
 
 
 
Markus Moll
Guest
Posts: n/a
 
      11-07-2006
Hi

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-07-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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

 
Reply With Quote
 
ninapeng728@gmail.com
Guest
Posts: n/a
 
      11-07-2006
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 写道:
"
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> >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


 
Reply With Quote
 
emil
Guest
Posts: n/a
 
      11-07-2006

(E-Mail Removed) 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

 
Reply With Quote
 
ninapeng728@gmail.com
Guest
Posts: n/a
 
      11-07-2006
It's a solution, but considering the efficiency, I prefer to use stream
than fscanf.


"emil 写道:
"
> (E-Mail Removed) 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


 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      11-07-2006
Hi

(E-Mail Removed) 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

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-07-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>thanks!

NB: please don't top-post, see
http://www.parashift.com/c++-faq-lit...t.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[i];

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fastest way to get minimum of four integer values? anders.johansen@gmail.com C++ 14 04-09-2008 10:28 AM
ifstream reading integer problem yingmu C++ 1 11-14-2006 03:42 PM
Q: formatted integer extraction from ifstream James Lehman C++ 1 04-23-2006 06:25 PM
get values from data file to integer variables.. jm0 C++ 4 02-05-2006 07:36 AM
check for consecutive integer values, ordered values GIMME Javascript 1 04-09-2004 01:13 PM



Advertisments