arido4
June 12th, 2008, 07:06 PM
Hello,
I have just started studying C++, come from a Java background, and need to develop a routine to read from a binary file different sizes of data (read a char, read an int, read a short, etc).
C++ STL offers me fstream.read ( char* s, streamsize n );
It is not a problem to read a char using this function: just pass the destination char and it is done.
However, I am trying to read an int from this function. I tried this:
char intToBeRead[4];
fstream.read(intToBeRead, 4);
Okay, now I got the 4 bytes that make up an integer. Now, how do I convert this char array to an int?
int intvalue = (int) intToBeRead;
doesn't seem to work.
I apologize if this is a stupid question. I know the solution must be really simple. Any help will be appreciated.
Thanks
I have just started studying C++, come from a Java background, and need to develop a routine to read from a binary file different sizes of data (read a char, read an int, read a short, etc).
C++ STL offers me fstream.read ( char* s, streamsize n );
It is not a problem to read a char using this function: just pass the destination char and it is done.
However, I am trying to read an int from this function. I tried this:
char intToBeRead[4];
fstream.read(intToBeRead, 4);
Okay, now I got the 4 bytes that make up an integer. Now, how do I convert this char array to an int?
int intvalue = (int) intToBeRead;
doesn't seem to work.
I apologize if this is a stupid question. I know the solution must be really simple. Any help will be appreciated.
Thanks