473,385 Members | 1,528 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,385 software developers and data experts.

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 6069

"dumboo" <vt***@yahoo.com> 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.com> 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
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 |...
3
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
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...
1
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...
6
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
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...
6
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
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();...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.