problem with file handling
Hi I have a txt file that I want my program to read. I use fstream. The file has the number 15 as the only thing in it. I want my program to read that 15 as an integer instead of a character string. Is that possible how do I convert a char string into an int. I know about typecasting but that doesn't work because it turns it into an integer that is huge and not the number 15. I think that is because it converts the string into the number of bits it took to store that string but I am not sure. So really my question is how do I get char thing[256]="15" to convert to int thingy = 15 ?
Am I making sense? Thank you.
Re: problem with file handling
Quote:
Originally posted by gell10
I think that is because it converts the string into the number of bits it took to store that string but I am not sure.
No. If you have this:
char * array = new char[256];
or this:
char array[256];
then "array", by itself, is a pointer to the first element of the array. So array is a variable that stores the memory address of the first element of the array. When you cast to an int, you get the address of the array.
Re: problem with file handling
Quote:
Originally posted by gell10
I use fstream. The file has the number 15 as the only thing in it. I want my program to read that 15 as an integer instead of a character string.
Come to think of it, isnt it simpler still to use:
int num;
in>>num; //assuming "in" is ifstream opened
i.e. read it as an integer straight away, rather than using atoi(), which I thought stood for "alpha to integer" rather than "array to integer".