Re: Streaming file IO and binary files
On Jul 25, 8:51 am, masood.iq...@lycos.com wrote:
Kindly excuse my novice question. In all the literature on ifstream
that I have seen, nowhere have I read what happens if you try to read
a binary file using the ">>" operator. I ran into the two problems
while trying to read a binary file.
Attention. The ">>" operator means "parse the next characters
in the file into the target type, interpreting them as text".
More generally, the abstraction of istream is that of a
transparent stream of characters (not raw bytes). All binary
does is control the interface with the OS.
1). All whitespace characters were skipped
Did you reset the skipws flag? If not, that's what you asked it
to do.
2). Certain binary files gave a core dump
Then there's a bug in your library. Good code never core dumps,
regardless of input. (Of course, the bug may simply be that you
forgot to replace the new handler, and aren't catching
bad_alloc. If you're reading into a string, for example, and
the input data contains a couple of GB without any white space,
something is going to give.)
The problems went away when I used the read() member function on the
input file stream instead. Is this the right way to go about?
It depends what you want to do. Read is good when you know you
have a block bytes of fixed size, with some special, possibly
non-text, format.
I was able to recreate my problem using simple sample source as below:
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, char* argv[])
Just a nit, but "implicit int" was removed from C++ a long, long
time ago. This shouldn't compile without a return type for
main.
{
if(argc < 2)
{
cerr << "usage: " << argv[0] << " <input-file>\n";
return 1;
}
ifstream ifs(argv[1], ios::in|ios::binary);
char ch;
size_t bytesRead = 0;
while(ifs)
{
ifs >> ch;
//ifs.read(&ch, 1);
bytesRead ++;
}
If the above loop ever core dumps, you should file a bug report.
If you want to just read characters, I'd use get:
while ( ifs.get( ch ) ) {
++ bytesRead ;
}
(Note too that your loop also counts one too many. For an empty
file, for example, it will count 1.)
Read is really for buffers which you will later unformat
yourself.
cout << "Successfully read " << bytesRead << " bytes\n";
return 0;
}
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34