Dear all,
I can use c language function fscanf() to get a char. string.
Ex:
FILE *file=fopen("te st.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. 7 19630
cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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;
cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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
"Noah Roberts" <nr******@donte mailme.com> ?????
news:3F******** ******@dontemai lme.com... cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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.
"Gianni Mariani" <gi*******@mari ani.ws> ?????
news:bk******** @dispatch.conce ntric.net... cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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.
Noah Roberts wrote: cylin wrote:
Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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
"cylin" <cy***@avant.co m.tw> wrote in message
news:bk******** ***@ID-154203.news.uni-berlin.de... "Gianni Mariani" <gi*******@mari ani.ws> ????? news:bk******** @dispatch.conce ntric.net... cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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
cylin wrote: Dear all,
I can use c language function fscanf() to get a char. string. Ex: FILE *file=fopen("te st.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 using ifstream?
-->fscanf(AddressFile, "%x", next_address);
if(AddressFile.eof()) {
return 0;
|
by: Jacek Dziedzic |
last post by:
Hi!
Consider the following program
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("test.txt");
|
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 main()
|
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 code in an attempt to
isolate the issue. The actually binary is 25kb, however the length of
the string appears to be only around 5kb. How can this be possible?
ifstream myfile( argv );
string fc;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |