reading/writing bytes??

I can write bytes to a file using:
1
2
3
4
int byte=5;//Random byte number.. :P
FILE *o=fopen("test.txt","wb");
fputc(byte,o);
fclose(o);

And it will save a byte(5) to a file, as i wanted it to do.

BUT now how do i read the file and get the number 5 out of it, i've tried something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
const int MAX_FILE_SIZE=255;
unsigned char buffer[MAX_FILE_SIZE];
string n;
FILE *f = fopen("test.txt", "rb");
if (f)
{
n = fread(buffer, MAX_FILE_SIZE, 1, f);
}
else
{
// error opening file
}
cout << n << endl;


But this just returns the letter, i wan't to be able to do this after i retrieve the byte:
1
2
3
4
5
int rs=#;//This will be what i get out from reading the text file with byte(5) in it.
if(rs==5)
{
//success!!!
}


This doesn't actually work though, instead of getting byte(5) out of the file that i saved byte(5) to, i just get some stange looking letter (obviously thats what byte(5) looks like in a string)

My main question is, how can i get the byte ID from a single character out of a text document :P

Or am i doing this completely wrong?? :)
Last edited on
The opposite of fputc is fgetc.

 
int rs = fgetc(f);
Topic archived. No new replies allowed.