M
masood.iqbal
Hi,
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.
1). All whitespace characters were skipped
2). Certain binary files gave a core dump
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?
I was able to recreate my problem using simple sample source as below:
Thanks,
Saleem
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, char* argv[])
{
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 ++;
}
cout << "Successfully read " << bytesRead << " bytes\n";
return 0;
}
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.
1). All whitespace characters were skipped
2). Certain binary files gave a core dump
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?
I was able to recreate my problem using simple sample source as below:
Thanks,
Saleem
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, char* argv[])
{
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 ++;
}
cout << "Successfully read " << bytesRead << " bytes\n";
return 0;
}