Problem writing ASCII characters in file
Hi,
I wrote a program to convert decimal numbers 0 -254 to characters and write it in a file. The program reads the characters back and gives the corresponding decimal numbers. But when I write corresponding characters of numbers 10,11,12, and 13, the program reads them back as 204. I cannot figure out how to get 10,11, 12 and 13 back. Any help will be appreciated. Here's the code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{ int x = 11;
ofstream outfile ("output.dat", ios::out);
if (!outfile)
{
exit (1);
}
outfile<<( unsigned char)(x);
outfile.close();
int p;
char ch;
unsigned char ch1;
ifstream infile;
infile.open("output.dat");
if (!infile)
{
exit (1);
}
while ((ch= infile.peek()) !=EOF )
{
infile >> ch1;
p = int (ch1);
cout<<p<<endl;
}
infile.close();
return 0;
}