473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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();
------------------------------------------------------
Thanks for your help.

Best Regards,
cylin.


Jul 19 '05 #1
7 19573
cylin wrote:
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;

Jul 19 '05 #2
cylin wrote:
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

Jul 19 '05 #3

"Noah Roberts" <nr******@dontemailme.com> ?????
news:3F**************@dontemailme.com...
cylin wrote:
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.
Jul 19 '05 #4

"Gianni Mariani" <gi*******@mariani.ws> ?????
news:bk********@dispatch.concentric.net...
cylin wrote:
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.
Jul 19 '05 #5
Noah Roberts wrote:
cylin wrote:
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.)
fscanf(file,"%s",data);
What does `data' contain after a failed read?
...
}
fclose(file);

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? ;-)

--
Artie Gold -- Austin, Texas

Jul 19 '05 #6

"cylin" <cy***@avant.com.tw> wrote in message
news:bk***********@ID-154203.news.uni-berlin.de...

"Gianni Mariani" <gi*******@mariani.ws> ?????
news:bk********@dispatch.concentric.net...
cylin wrote:
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
Jul 19 '05 #7
cylin wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: John Phung | last post by:
Is there a fscanf equivalent for c++? Here's what I'm talking about: unsigned int NextAddress(ifstream& AddressFile) { unsigned int next_address; -->How do I rewrite the fscanf listed below...
5
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
1
by: Alex Vinokur | last post by:
I have several questins concerning a program below. ------ foo.cpp : BEGIN ------ #include <cassert> #include <cstdlib> #include <iostream> #include <fstream> using namespace std; int...
3
by: farseer | last post by:
Hi, Hi, i am reading the content of a binary file, enoding as base64 and writing to an output file. After noticing that my output file seems to be truncated, i created the following simple test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.