get integer values using ifstream error.

Z

zhouchengly

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
 
I

Ivan Vecerina

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
 
N

ninapeng728

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

emil

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
 
N

ninapeng728

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


"emil дµÀ£º
"
 
I

Ivan Vecerina

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...
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top