better way to read data?

K

kathy

I try to read a text file with format:

11 1.11111
22 2.22222
33 3.33333
....

I try to use fscanf() to read the data back. What is the better way?
 
A

Alf P. Steinbach

* kathy:
I try to read a text file with format:

11 1.11111
22 2.22222
33 3.33333
...

I try to use fscanf() to read the data back. What is the better way?

Depends on your criteria.

Have you tried C++ streams, like std::cin and (type) std::ifstream?
 
V

Victor Bazarov

kathy said:
I try to read a text file with format:

11 1.11111
22 2.22222
33 3.33333
...

And what do you expect after reading? A bunch of integers and floats?
A bunch of strings? A bunch of pairs (triples) of strings? What?
I try to use fscanf() to read the data back. What is the better way?

In C++ the use of operator>> with 'ifstream' is encouraged. Whether it
is actually better is a matter of personal preference.

V
 
M

mlimber

Victor said:
kathy wrote: [snip]
I try to use fscanf() to read the data back. What is the better way?

In C++ the use of operator>> with 'ifstream' is encouraged. Whether it
is actually better is a matter of personal preference.

Well, I'd say it's more than a matter of personal preference since
*scanf() are not type-safe.

char c;
int d;
scanf( "%d %c", &c, &d ); // Oops!

Whereas code with iostreams is less hazardous to your program's health
since the ellipsis is not used and since the format strings do not need
to be correlated to the arguments:

cin >> d >> c; // No oops

Cheers! --M
 
V

Victor Bazarov

mlimber said:
Victor said:
kathy wrote:
[snip]
I try to use fscanf() to read the data back. What is the better way?

In C++ the use of operator>> with 'ifstream' is encouraged. Whether it
is actually better is a matter of personal preference.


Well, I'd say it's more than a matter of personal preference since
*scanf() are not type-safe.

C++ and C are so filled with UB or IDB, that using them in general is
a certain risk, and taking such risk _is_ a matter of personal preference.
char c;
int d;
scanf( "%d %c", &c, &d ); // Oops!

Whereas code with iostreams is less hazardous to your program's health
since the ellipsis is not used and since the format strings do not need
to be correlated to the arguments:

cin >> d >> c; // No oops

Cheers! --M

V
 
M

Mike Wahler

mlimber said:
Victor said:
kathy wrote: [snip]
I try to use fscanf() to read the data back. What is the better way?

In C++ the use of operator>> with 'ifstream' is encouraged. Whether it
is actually better is a matter of personal preference.

Well, I'd say it's more than a matter of personal preference since
*scanf() are not type-safe.

char c;
int d;
scanf( "%d %c", &c, &d ); // Oops!

Whereas code with iostreams is less hazardous to your program's health
since the ellipsis is not used and since the format strings do not need
to be correlated to the arguments:

cin >> d >> c; // No oops

Unless the data are not in the same order the
input statements expect. Moral: always check
for errors, whether using scanf() or operator>>().

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top