Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# How to read in a single digit integer from file

Posted on 2003-03-15
Medium Priority
237 Views
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
0
Question by:lordiano

LVL 2

Accepted Solution

limestar earned 105 total points
ID: 8142833
What you want is actually to read in one character at a time. When you have a character, say '5', it can easily be converted to an integer with the value 5 by doing something like this:

char digitChar = '5';
int digit = digitChar - '0';

Things will go wrong if you read an 'A' or something, but if you know your input is '0'-'9', then the above method works fine.

Now that you have a method for converting a character to a digit it's up to you if you want to read one character at a time or read them all into an array first.
0

Author Comment

ID: 8142841
How do I read 1 character at a time?
0

Author Comment

ID: 8142853
How do I read 1 character at a time?
0

Expert Comment

ID: 8142863
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.
0

LVL 4

Expert Comment

ID: 8142899
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.
0

Expert Comment

ID: 8142938
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.
0

Author Comment

ID: 8142956
Thanks fivesigmaevent and n_fortynine for your input.
Appreciate the help, thanks again
And Limestar, thanks for your comment it works flawlessly.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. â€¦
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month10 days, left to enroll