Hi Everybody.
Was hoping someone could clear this up for me. My understanding is not good in this area.
The below code simply reads a string from a text file two characters at a time. It then outputs them to console then casts them to an int and then outputs them to the console again.
Where I have the cast to int each number comes out different as expected. But.. for the below line, 1 - does the below only output the int value for the first character (remembering that two values drawn into the char string)?
and
**2 - ** when I change the cast to the below. Why do all the numbers output to the console come out the same?
int mint = (int)mychar;
int *mint = (int*)mychar;
cout << *mint << endl;
Is this the correct way to display the int value for two characters in the char string?
int main()
{
ifstream myfile;
stringstream ss;
myfile.open("test.txt", ios::out |ios::binary);
char *mychar = new char[2];
while ((myfile.read(mychar,2)) )
{
cout << mychar << endl;
int *mint = (int*)mychar;
cout << *mint << endl;
}
myfile.close();
}