thanks to all of you.
whiteflags: thanks for the suggestions. It seems, though, that
Code:
fin.read( (char*)(&inbuff), 1 );
makes more sense for non-printable data. I tried "fin >>" but it has a VERY STRANGE & PUZZLING RESULT (see below).
Codeplug: does Windows distinguish the two file types strictly based on their extensions, or does the OS use some sort of file metadata written to the disk that we don't have access to?
tabstop: I had in mind situations in which there aren't two people to discuss the file, i.e. some sort of agent program, bot, etc., that acquires a file & has to determine what it contains with no outside assistance. By the way, I'm not sure what you had in mind regarding 0xff and 0x7f. I don't see any unusual results for those values.
**************
This code gives me (in Linux) the same output no matter how it is opened. I changed the extensions for the 2 cases in which it is opened for writing as a text file, in case anyone wants to see what that does (if anything) in Windows.
Notice that when I use "fin >>" to read in '13' '10' something odd happens to inbuff, with the result that in each case the first call to
Code:
cout << " " << (int)inbuff;
has no output -- not even the blank space. In the last test I replaced cout with printf, which acts even more strangely. It does nothing for the first "13 10", but executes a linefeed (or a CR/LF) for the second "13 10". But inbuff is just an unsigned char. It can only hold values [0, 255], and how can one '13' be different from another '13'? Anybody know what's happening here?
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
// int intval1 = 1919006563; // this is what I should get back when the chars are put together
// int intval2, intval3;
unsigned char cbuff[4];
unsigned short sbuff[1];
unsigned char inbuff;
void* vp;
cout << "First test - file opened as binary for writing, opened as binary for reading:\n";
ofstream fout( "iotest.dat", ios::binary );
if( !fout.is_open() ) {
cout << "couldn't open for output\n";
return 1;
}
cbuff[0] = (unsigned char)13;
cbuff[1] = (unsigned char)10;
cbuff[2] = (unsigned char)255;
cbuff[3] = (unsigned char)127;
sbuff[0] = (unsigned short)0xa0d;
vp = cbuff;
fout.write( (const char*)vp, 4 );
vp = sbuff;
fout.write( (const char*)vp, 2 );
fout.close();
ifstream fin( "iotest.dat", ios::binary );
if( !fin.is_open() ) {
cout << "couldn't open for input\n";
return 1;
}
inbuff = 99;
cout << "inbuff contents before input: " << inbuff << endl;
cout << "formatted input:";
fin >> inbuff;
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin >> inbuff;
}
cout << "." << endl;
fin.clear();
fin.seekg(0);
cout << "unformatted input:";
fin.read( (char*)(&inbuff), 1 );
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin.read( (char*)(&inbuff), 1 );
}
cout << "." << endl;
fin.close();
cout << "Second test - file opened as binary for writing, opened as text for reading:\n";
fout.open( "iotest.dat", ios::binary );
if( !fout.is_open() ) {
cout << "couldn't open for output\n";
return 1;
}
cbuff[0] = (unsigned char)13;
cbuff[1] = (unsigned char)10;
cbuff[2] = (unsigned char)255;
cbuff[3] = (unsigned char)127;
sbuff[0] = (unsigned short)0xa0d;
vp = cbuff;
fout.write( (const char*)vp, 4 );
vp = sbuff;
fout.write( (const char*)vp, 2 );
fout.close();
fin.open( "iotest.dat" );
if( !fin.is_open() ) {
cout << "couldn't open for input\n";
return 1;
}
inbuff = 99;
cout << "inbuff contents before input: " << inbuff << endl;
cout << "formatted input:";
fin >> inbuff;
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin >> inbuff;
}
cout << "." << endl;
fin.clear();
fin.seekg(0);
cout << "unformatted input:";
fin.read( (char*)(&inbuff), 1 );
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin.read( (char*)(&inbuff), 1 );
}
cout << "." << endl;
fin.close();
cout << "Third test - file opened as text for writing, opened as binary for reading:\n";
fout.open( "iotest.txt" );
if( !fout.is_open() ) {
cout << "couldn't open for output\n";
return 1;
}
cbuff[0] = (unsigned char)13;
cbuff[1] = (unsigned char)10;
cbuff[2] = (unsigned char)255;
cbuff[3] = (unsigned char)127;
sbuff[0] = (unsigned short)0xa0d;
vp = cbuff;
fout.write( (const char*)vp, 4 );
vp = sbuff;
fout.write( (const char*)vp, 2 );
fout.close();
fin.open( "iotest.dat", ios::binary );
if( !fin.is_open() ) {
cout << "couldn't open for input\n";
return 1;
}
inbuff = 99;
cout << "inbuff contents before input: " << inbuff << endl;
cout << "formatted input:";
fin >> inbuff;
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin >> inbuff;
}
cout << "." << endl;
fin.clear();
fin.seekg(0);
cout << "unformatted input:";
fin.read( (char*)(&inbuff), 1 );
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin.read( (char*)(&inbuff), 1 );
}
cout << "." << endl;
fin.close();
cout << "Fourth test - file opened as text for writing, opened as text for reading:\n";
fout.open( "iotest.txt" );
if( !fout.is_open() ) {
cout << "couldn't open for output\n";
return 1;
}
cbuff[0] = (unsigned char)13;
cbuff[1] = (unsigned char)10;
cbuff[2] = (unsigned char)255;
cbuff[3] = (unsigned char)127;
sbuff[0] = (unsigned short)0xa0d;
vp = cbuff;
fout.write( (const char*)vp, 4 );
vp = sbuff;
fout.write( (const char*)vp, 2 );
fout.close();
fin.open( "iotest.dat" );
if( !fin.is_open() ) {
cout << "couldn't open for input\n";
return 1;
}
inbuff = 99;
cout << "inbuff contents before input: " << inbuff << endl;
cout << "formatted input:";
fin >> inbuff;
while( !fin.eof() ) {
printf(" %d\n", inbuff);
// cout << " " << (int)inbuff;
fin >> inbuff;
}
cout << "." << endl;
fin.clear();
fin.seekg(0);
cout << "unformatted input:";
fin.read( (char*)(&inbuff), 1 );
while( !fin.eof() ) {
cout << " " << (int)inbuff;
fin.read( (char*)(&inbuff), 1 );
}
cout << "." << endl;
fin.close();
}