By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

get integer values using ifstream error.

P: n/a
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 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi

zh*********@gmail.com 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

Nov 7 '06 #2

P: n/a
<zh*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro 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<ItemTypeInItem;
std::vector<ItemTypeconst 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 '06 #3

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

"Ivan Vecerina 写道:
"
<zh*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro 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<ItemTypeInItem;
std::vector<ItemTypeconst 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 '06 #4

P: n/a

zh*********@gmail.com 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 '06 #5

P: n/a
It's a solution, but considering the efficiency, I prefer to use stream
than fscanf.
"emil 写道:
"
zh*********@gmail.com 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 '06 #6

P: n/a
Hi

ni*********@gmail.com 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

Nov 7 '06 #7

P: n/a
<ni*********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro 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<ItemTypeInItem;
std::vector<ItemTypeconst 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<ItemTypeitems(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<ItemTypeitems;
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<ItemTypeconst 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

Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.