5 Replies - 819 Views - Last Post: 11 October 2010 - 08:21 AM Rate Topic: -----

#1 philosophyincode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 28-April 09

Reading in longs from a file in c++

Posted 11 October 2010 - 01:35 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Reading in longs from a file in c++

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1653
  • View blog
  • Posts: 3,129
  • Joined: 30-May 10

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
Was This Post Helpful? 1
  • +
  • -

#3 Mohanddo  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 07-July 09

Re: Reading in longs from a file in c++

Posted 11 October 2010 - 05:49 AM

Create a char buffer, use fscanf with %s type specifier.

Then use atol() to convert it from char to an int.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading in longs from a file in c++

Posted 11 October 2010 - 06:47 AM

View PostMohanddo, on 11 October 2010 - 09:49 PM, said:

Create a char buffer, use fscanf with %s type specifier.

Then use atol() to convert it from char to an int.


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/
Was This Post Helpful? 4
  • +
  • -

#5 Mohanddo  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 07-July 09

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/


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

Was This Post Helpful? 0
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Reading in longs from a file in c++

Posted 11 October 2010 - 08:21 AM

View PostMohanddo, 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1