Xian wrote in message ...
Is it that the \r on a windows system is discarded when it is read in
in
a \r\n pair? And only for files open in ASCII mode?
Try this:
// ------------
#include <iostream> // #include <ostream>
#include <string>
#include <vector>
int main(){ using std::cout; // for NG post
// put the name of a *short* file in the next string.
std::string FileName("ZZtest.txt");
// 1
std::ifstream sfin(FileName.c_str(),
std::ios_base::in|std::ios_base::binary );
// 2
// std::ifstream sfin( FileName.c_str() ); // try this second
if( not sfin.is_open() ){
cout<<"\n ifstream sfin FAILED"<<std::endl;
return 1; // EXIT_FAILURE
}
std::vector<unsigned char> Image;
char In(0);
while( sfin.get( In ) ){
Image.push_back( static_cast<unsigned char>( In ) );
}
sfin.close();
cout<<"\n Image.size() = "<<Image.size()<<" bytes."<<std::endl;
for( size_t i(0); i < Image.size(); ++i ){
int out( static_cast<int>( Image.at( i ) ) );
cout<<std::setw(3)<<out<<" ";
if((i>0)&&(i%10) == 0){ cout<<std::endl;}
}
cout<<std::endl;
return 0;
} // main()
// ------------
[ Are you on a windows machine? (You may get different output.) ]
In the output, look for "13 10". That's the cr-lf pair. (ANSI ASCII)
Now try (un-comment 2, comment 1) the second file-open version
(non-binary). Still see the '13'?
// ------------
int out( static_cast<int>('\n') );
cout<<"'\\n'="<<std::setw(3)<<out<<std::endl;
out = static_cast<int>('\r');
cout<<"'\\r'="<<std::setw(3)<<out<<std::endl;
/* - output - (MinGW GCC)
'\n'= 10
'\r'= 13
*/
// ------------
you would recommend reading?
Sites: Dinkumware, Boost.
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
*THE* FAQ has lots of really good stuff:
The comp.lang.c++ FAQ is available at
http://www.parashift.com/c++-faq-lite/