473,608 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fstream::read(. .);

hi there
i m using fstream object for reading through and writing to my file, using
following code

char c;
while(!m_aa.eof ())
{
m_aa.read(&c, sizeof(c));
m_oo.write(&c, sizeof(c));

cout<<"\nout to c:"<<(int)c;
}

when i saw the outoput of 'cout' and checked the output in HEX editor,
it always showed me 0xCC (-52) in first byte(which is not present in input
file), i dont know why is it occouring, i had open the files properly in
binary mode, and m not doing any kind of preprocessing, except the above
operation,
I m using MSVC 6.0 any suggestion ?
Jul 22 '05 #1
2 6079

"dumboo" <vt***@yahoo.co m> wrote in message
news:2g******** ****@uni-berlin.de...
hi there
i m using fstream object for reading through and writing to my file, using
following code

char c;
while(!m_aa.eof ())
{
m_aa.read(&c, sizeof(c));
m_oo.write(&c, sizeof(c));

cout<<"\nout to c:"<<(int)c;
}

when i saw the outoput of 'cout' and checked the output in HEX editor,
it always showed me 0xCC (-52) in first byte(which is not present in input
file),
In the first byte? I could understand it in the last byte, because your
while loop is wrong.

I don't know why so many people get this wrong, but eof is not true when you
are at the end of a file. eof is used to test after a read has be done
whether that read failed because you *were* at the end of a file. So in your
code the following happens

1) read upto end of file
2) at end of file, eof returns false
3) call read, fails, because you were at the end of file
4) output garbage value of c
5) now eof is true, quit loop.

The right way to write this sort of loop is

while (m_aa.read(&c, sizeof(c))
{
m_oo.write(&c, sizeof(c));
cout<<"\nout to c:"<<(int)c;
}
if (m_aa.eof())
{
// ok we hit end of file
}
else
{
// some other sort of error
}
i dont know why is it occouring, i had open the files properly in
binary mode, and m not doing any kind of preprocessing, except the above
operation,
I m using MSVC 6.0 any suggestion ?


I'm not sure, the problem I described above doesn't exactly match what you
are describing. I would suggest posting a complete compilable program that
demonstrates this behaviour. Possibly the error is in the code you haven't
posted so far. In any case make the improvement I suggested above.

john
Jul 22 '05 #2
hi there
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2g******** ****@uni-berlin.de...

"dumboo" <vt***@yahoo.co m> wrote in message
news:2g******** ****@uni-berlin.de...
hi there
[...] while (m_aa.read(&c, sizeof(c))
{
m_oo.write(&c, sizeof(c));
cout<<"\nout to c:"<<(int)c;
}
if (m_aa.eof())
{
// ok we hit end of file
}
else
{
// some other sort of error
}

[...]
Thanks John that solved the problem :-)
Jul 22 '05 #3

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

Similar topics

8
9842
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
3
2263
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
9
5283
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream since I will need to write and read from files, and I am using those files as binary files. I made a sample of what's going on (below). Without using vector, everything works fine, but with using vectors, something is going wrong somewhere!?? ...
1
5345
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data doesn't show in the next read, or when I open the file itself. Additionally, I get a insufficient parameters error from if I remove the ios flags from the open--from what I have read, isn't fstream supposed to default the flags to ios::in|ios::out? I...
6
17119
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
0
1800
by: lion | last post by:
I have in my programm three files: SCANNER.CPP,tst_scanner.cpp and scaner.h i need read and write from text file which is glopal. how i can do it? the strange things if i put all programm in one file it is working as good.but whene i use three files it is have error(not define the glopal text file in some file). i am using many wayes to solve this probleme(i define the glopal text file out of main or enter and enter of getT() or outer of...
6
3727
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
1
1833
by: aviraldg | last post by:
I'm having a problem with read , my code is somewhat like: char chipMemory ; // ~35XX fstream ifile; ifile.open(argv,ios::in|ios::binary|ios::nocreate|ios::ate); int fileLength=ifile.tellg(); ifile.seekg(ios::beg); ifile.read(chipMemory,fileLength); ifile.close(); After the last function call in the program, (Note , this is an extract , and seemingly irrelevant pieces have been removed) (short) chipMemory "prints as" (printf used)...
1
3922
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
8013
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8488
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8164
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,...
0
8360
tracyyun
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...
1
6017
isladogs
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...
0
5489
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
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...
1
2482
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
0
1345
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.