How to read in a single digit integer from file

Posted on 2003-03-15
Medium Priority
Last Modified: 2012-05-05
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
Question by:lordiano
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.

Author Comment

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

Author Comment

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8142863

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.

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.

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.

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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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. …
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question