[Dev-C++] Sequence not recorded/read. Why?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Roger C. <rc...@ic...> - 2003-10-01 17:25:51
|
Dear sir,
I tried the discussion list with no success. So, excuse me for coming to you. I
do not know if I have a program error or a compiler error. Anyway, I can see no
error on my source code so, please, have a look on it. The source code
presented is the smallest code that I could write in order to show the problem.
The program records 4 times the sequence 9 a b c d, then close the file, open
again and reads its contents which should be 4 times the sequence 9 a b c d.
But it does not happen - it seems that the program reads nothing.
Then, the program records 4 times the sequence 1 2 3 4 5, then close the file,
open again and reads its contents which should be 4 times the sequence 1 2 3 4
5. Perfect, the program reads the file contents as expected.
I could realize that, considering the sequence 0 to ff, the program does not
read the bytes 9 a b c d 20. There is no problem with the other bytes. I do not
understand why.
It follows bellow the source code to make tests. The part bellow the
line //////////////////////////// is identical to the part above this line with
only one difference to define the start number (1 or 9). The line is:
ch = 9 + (i%5); // generates 01 byte (9 to d)
above this line
ch = 1 + (i%5); // generates 01 byte (1 to 5)
bellow this line
Thank you for your help
Source code:
#include <iostream>
using namespace std;
#include <fstream>
using namespace std;
main()
{
unsigned char ch;
cout << endl;
// open file for output
ofstream arquivo("binary", ios::out|ios::binary);
// copy bytes to output file
for (int i=0; i<20; i++) {
ch = 9 + (i%5); // generates 01 byte (9 to d)
arquivo << ch; // writes 01 byte to out file
cout.width(3); // shows the recorded byte
cout << hex << int(ch);
}
arquivo.close(); //finished, close the file.
cout << endl;
ch=0; ////// Wrong, it shows this number as result of cout bellow. Why?
// open file for input
ifstream arquivo0("binary", ios::in|ios::binary);
// read bytes from output file
for (int i=0; i<20; i++)
{
arquivo0 >> ch; // reads 01 byte from out file
cout.width(3); // shows the read byte
cout << hex << int(ch); // it seems that it did not read the byte from
file!!!
}
arquivo0.close(); //finished, close the file.
///////////////////////////////////////////////////////////////////////////////
cout << endl;
// open file for output
ofstream arquivo1("binary", ios::out|ios::binary);
// copy bytes to output file
for (int i=0; i<20; i++) {
ch = 1 + (i%5); // generates 01 byte (1 to 5)
arquivo1 << ch; // writes 01 byte to out file
cout.width(3); // shows the recorded byte
cout << hex << int(ch);
}
arquivo1.close(); //finished, close the file.
cout << endl;
ch=0; ////// As expected, it does not shows this number as result of cout
bellow
// open file for input
ifstream arquivo2("binary", ios::in|ios::binary);
// read bytes from output file
for (int i=0; i<20; i++)
{
arquivo2 >> ch; // reads 01 byte from out file
cout.width(3); // shows the read byte
cout << hex << int(ch);
}
arquivo2.close(); //finished, close the file.
}
------------------------------------------
Mensagem enviada atraves do WebMail ICONet
|