Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading integers from text file

Status
Not open for further replies.

grnfvr

MIS
Dec 21, 2000
111
0
0
US
the only way i know to get data out of a text file is with the getline function on a fstream object. one line in my text file contains two integers. is there a way i can read in the integers? or can i convert the line with two integers i get using getline into two integers?
 
Hi,

the easiest way is to use
int i,j;
FILE *stream;
fscanf( stream, "%d %d", &i, &j ) ;

--------------------------------------------------
The best is to use fgets ( or .getline) + sscanf :

char buf[80];
fgets( buf, 70, stream );
or
x.getline( buf, 70, '\n')

sscanf( buf, "%d %d", &i, &j ) ;

----------------------------------------------

Reading the buffer before scanf, brings your prog. to
be more flex. and relab. ( you can test len of line, mix
diff ype of elem in just one read op. and then make conversion.)

bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top