How can I read one number at a time from an int file?

Hi all,
I have a file called example.txt . In that file, the int 123456 is stored. How can I read one independent number from that int? Lets say, I have an int variable called "Weight." How do I set weight equal to the number 1 from the int 123456 in the file?
Any help is appreciated. Thanks a lot!
1
2
ifstream data("Data.dat");
int weight = data.get() - '0';


http://www.cplusplus.com/reference/istream/istream/get/

Edit:
Forgot to convert char to int.
Last edited on
Forgot to convert char to int


a char is an int
I meant converting '0' to 0, '1' to 1, etc., not the ASCII value.

If you look at the ASCII table you will see that the ASCII code for the numeric values [0:9] are [48:57]:

http://www.asciitable.com/

So if you deduct 48 from the char value you will get the desired result:

1
2
3
4
5
6
int main()
{
   char c('1');
   int x = c - 48;
   return 0;
}


Does that help you now?
Topic archived. No new replies allowed.