Link to home
Start Free TrialLog in
Avatar of lordiano
lordiano

asked on

How to read in a single digit integer from file

I have a file of a very long integer like : 43242348913123141341321487398432
How do I using ifstream to read in only 1 digit at a time?
I tried
inFile >> number; but i think it reads in the whole line and does not give me a single digit number.
I have tried using getline first to store it in array, but I cannot get the correct integer value from the array because they'd be stored in char.

Is there a simple way to get just 1 digit at a time, without having to store it in char array then somehow convert it back to int again?

thank you very much
ASKER CERTIFIED SOLUTION
Avatar of limestar
limestar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lordiano
lordiano

ASKER

How do I read 1 character at a time?
How do I read 1 character at a time?
lordiano,

That number in your file is much too large to store in an int. A signed 32 bit integer can only hold a number as large as 2147483648 and 32 bit unsigned int is double that.

For extremely large numbers that require calculations, you can just retrieve the number as a string of characters and write a class with member functions that performs math on that long string of digit chars.

I know you wanted something simpler, but I don't think it's possible any other way with such large numbers.
fivesigmaevent's is a good suggestion. If you really just want to get one at a time, use cin.get(ch) with ch being a character, then convert that character to an int the way limestar said.
I guess I misunderstood what you are trying to accomplish exactly. From your first sentence

"I have a file of a very long integer like : 43242348913123141341321487398432"

It sounded like you wanted to store the entire value as an int. It is impossible to store this in a primitive int variable.
Thanks fivesigmaevent and n_fortynine for your input.
Appreciate the help, thanks again
And Limestar, thanks for your comment it works flawlessly.