Hello,
I am confused as to how to read in a file (of indeterminate size) of longs in c++.
Thus far, I figure something like
while(!inStream.eof())
{
//read in longs here
}
is the basis but I am not sure of what method to use to read in the longs from a file that has each one placed on a separate line. Ex. 89
98
100000000000
Any help would be appreciated. Thanks
Reading in longs from a file in c++
Page 1 of 15 Replies - 640 Views - Last Post: 11 October 2010 - 08:21 AM
Replies To: Reading in longs from a file in c++
#2
Re: Reading in longs from a file in c++
Posted 11 October 2010 - 01:45 AM
long int myVar;
while ( inStream >> myVar ) {
// do something
}
Normally, you should avoid using eof() in a loop test.
http://sourceforge.n....php?title=Feof
#3
Re: Reading in longs from a file in c++
Posted 11 October 2010 - 05:49 AM
#4
Re: Reading in longs from a file in c++
Posted 11 October 2010 - 06:47 AM
Mohanddo, on 11 October 2010 - 09:49 PM, said:
Right idea IMO but why do it in C code when we are writing in C++?
Use a string, not a char buffer.
http://www.cplusplus...ference/string/
Use getline(), not fscanf.
http://www.cplusplus...string/getline/
Use a stringstream, not atoi()
http://www.cplusplus...m/stringstream/
#5
Re: Reading in longs from a file in c++
Posted 11 October 2010 - 07:27 AM
Quote
Right idea IMO but why do it in C code when we are writing in C++?
Use a string, not a char buffer.
http://www.cplusplus...ference/string/
Use getline(), not fscanf.
http://www.cplusplus...string/getline/
Use a stringstream, not atoi()
http://www.cplusplus...m/stringstream/
Use a string, not a char buffer.
http://www.cplusplus...ference/string/
Use getline(), not fscanf.
http://www.cplusplus...string/getline/
Use a stringstream, not atoi()
http://www.cplusplus...m/stringstream/
Thanks.
Can i ask is there any particular problems with using c style functions in c++? I am much more comfortable managing files with C than C++.
This post has been edited by Mohanddo: 11 October 2010 - 07:32 AM
#6
Re: Reading in longs from a file in c++
Posted 11 October 2010 - 08:21 AM
Mohanddo, on 11 October 2010 - 04:57 PM, said:
Can i ask is there any particular problems with using c style functions in c++? I am much more comfortable managing files with C than C++.
Then you should write your programs in C. My question is, why not use what C++ provides you with? Why make things harder for yourself when the right tools are already within your grasp?
And also, here's a tutorial by bench, on reading files in C++ and the fact about eof.
This post has been edited by Anarion: 11 October 2010 - 08:26 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|