S
Steven T. Hatton
I know of a least one person who believes std::ifstream::read() and
std:fstream::write() are "mistakes". They seem to do the job I want
done. What's wrong with them. This is the code I currently have as a test
for using std::ifstream::read(). Is there anything wrong with the way I'm
getting the file?
#include <vector>
#include <iomanip>
#include <fstream>
#include <iostream>
template<typename Iterator>
std:stream& printHexLine(Iterator start, Iterator stop, std:stream& out)
{
while(start<stop) out
<<std::setw(2)
<<(static_cast<unsigned int>(static_cast<unsigned char>(*start++)))<<"
";
return out;
}
template<typename Container>
std:stream& print(const Container& data, std:stream& out) {
typedef typename Container::const_iterator c_itr;
std:stream hexout(out.rdbuf());
hexout.setf(std::ios::hex, std::ios::basefield);
hexout.fill('0');
c_itr from (data.begin());
c_itr dataEnd (from + data.size());
c_itr end (dataEnd - (data.size()%16));
for(c_itr start = from; start < end; start += 16) printHexLine(start,
start + 16, hexout)<<"\n";
printHexLine(end, dataEnd, hexout)<<"\n";
return out;
}
int main (int argc, char* argv[]) {
std::string filename("fileio");
std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary
std::ios::ate);
std::vector<char>vbuf(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&vbuf[0], vbuf.size());
print(vbuf, std::cout);
return 0;
}
std:fstream::write() are "mistakes". They seem to do the job I want
done. What's wrong with them. This is the code I currently have as a test
for using std::ifstream::read(). Is there anything wrong with the way I'm
getting the file?
#include <vector>
#include <iomanip>
#include <fstream>
#include <iostream>
template<typename Iterator>
std:stream& printHexLine(Iterator start, Iterator stop, std:stream& out)
{
while(start<stop) out
<<std::setw(2)
<<(static_cast<unsigned int>(static_cast<unsigned char>(*start++)))<<"
";
return out;
}
template<typename Container>
std:stream& print(const Container& data, std:stream& out) {
typedef typename Container::const_iterator c_itr;
std:stream hexout(out.rdbuf());
hexout.setf(std::ios::hex, std::ios::basefield);
hexout.fill('0');
c_itr from (data.begin());
c_itr dataEnd (from + data.size());
c_itr end (dataEnd - (data.size()%16));
for(c_itr start = from; start < end; start += 16) printHexLine(start,
start + 16, hexout)<<"\n";
printHexLine(end, dataEnd, hexout)<<"\n";
return out;
}
int main (int argc, char* argv[]) {
std::string filename("fileio");
std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary
std::ios::ate);
std::vector<char>vbuf(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&vbuf[0], vbuf.size());
print(vbuf, std::cout);
return 0;
}