How to use ifstream::get() like fscanf()?

C

cylin

Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);
...
}
fclose(file);
--------------------------------------------------------
But if using c++, how to get? I always get a whole line such as using
getline(), not a char. string.
Ex:
ifstream file;
file.open("test.txt",ios::in);
char data[100];
while (!file.eof()) {
file.get(????)------------------------------------------->????????????
...
}
file.close();
 
N

Noah Roberts

cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);
...
}
fclose(file);

If you want to read words then this works:

ifstream f(filename);
string in;

f >> in;
 
G

Gianni Mariani

cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);

great big nasty security hole right here ^^^^ !
...
}
fclose(file);
--------------------------------------------------------
But if using c++, how to get? I always get a whole line such as using
getline(), not a char. string.
Ex:
ifstream file;
file.open("test.txt",ios::in);
char data[100];
while (!file.eof()) {
file.get(????)------------------------------------------->????????????

file >> ....

WAIT, don't do file >> data as it will buffer overrun (just like above).

std::string strdata;

if ( file >> strdata ) ...

BTW - I'm sure you'll find the answer in the NG FAQ.

http://www.parashift.com/c++-faq-lite/input-output.html
 
C

cylin

Noah Roberts said:
cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);
...
}
fclose(file);

If you want to read words then this works:

ifstream f(filename);
string in;

f >> in;

Thanks....I got it.
 
C

cylin

Gianni Mariani said:
cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);

great big nasty security hole right here ^^^^ !

Thanks for your imformat....
It's good to handle end of file.

But if the file is binary, and have variant records.
How to hanlde without testing end of file?
Here is my method.
while(!file.eof()) {
decode record header...
decode record data ...
do something...
}
Is it ok?

Regards,
cylin.
 
A

Artie Gold

Noah said:
cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {

Erm, that isn't even right![1]
(feof() doesn't return 1 until *after* a read encounters end of file.)

What does `data' contain after a failed read?
If you want to read words then this works:

ifstream f(filename);
string in;

f >> in;

HTH,
--ag

[1] I can mention that here, right? ;-)
 
M

Mike Wahler

cylin said:
Gianni Mariani said:
cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);

great big nasty security hole right here ^^^^ !

Thanks for your imformat....
It's good to handle end of file.

But if the file is binary, and have variant records.
How to hanlde without testing end of file?
Here is my method.
while(!file.eof()) {
decode record header...
decode record data ...
do something...
}
Is it ok?

No. As was already stated 'eof()' does not return
true until after an attempt to read past end of file.
What you have will make it appear that the last
record was read twice.

while(stream >> object)
{
// do stuff
}

loop will terminate when 'stream' is in 'fail'
state. This state is caused by conversion failure
or eof. Now we distinguish between them:

if(stream.eof())
/* end of file reached */
else
/* conversion error */


-Mike
 
K

Kevin Goodsell

cylin said:
Dear all,

I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("test.txt","r");
char data[100];
while (!feof(file)) {
fscanf(file,"%s",data);
...
}
fclose(file);

You really should review both the comp.lang.c and comp.lang.c++ FAQ
lists. Your code has some rather serious problems (which have already
been pointed out), and the FAQs do a good job of explaining why and what
to do differently.

http://www.parashift.com/c++-faq-lite/
http://www.eskimo.com/~scs/C-faq/top.html

-Kevin
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top