473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

get integer values using ifstream error.

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

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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
2
by: Peter Gordon | last post by:
Is using getline with a fstream reference covered in the standard? I have three C++ compilers on my system. Two of them compile the code below without warnings. The third returns the following...
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
4
by: jm0 | last post by:
Hi, im developing this program, and then i ran into some trouble "oooh no!" hate to ask.. have search google and this page for something to spilt things up in arrays or whatever can be used, spent...
3
by: khaleel.alyasini | last post by:
Hi, currently I'm working on DCT algorithm on C++. I was wondering if anyone out there could point me out how to seperate the pixel values into DCT blocks. Here, i have enclosed my source...
1
by: James Lehman | last post by:
Hello. I want to write a program that reads AutoCAD shape (font) files. They are written with the convention that hexadecimal values have a leading zero and decimal values do not. All numbers...
4
by: Gary Wessle | last post by:
Hi I am writing a code to open a space delimited data file, return the number of rows and columns, as well as return the nth column where n is user define number. I put all the cells in a...
4
by: marathoner | last post by:
I tried your advice, and replaced "ifstream" with "std::ifstream". I also replaced instances of "ofstream" with "std::ofstream". Those syntax errors were resolved. When I added "std" to the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.