tags:

views:

67

answers:

3

Is there any way to get input from a file one number at a time? For example I want to store the following integer in an vector of integers since it is so long and can't be held by even a long long int.

12345678901234567900

So how can I read this number from a file so that I can:

vector<int> numbers;
number.push_back(/*>>number goes here<<*/)

I know that the above code isn't really complete but I hope that it explains what I am trying to do. Also I've tried google and so far it has proved innefective because only tutorials for C are coming up which aren't really helping me all too much.

Thank is advance, Dan Chevalier

A: 

You can read a char at a time with char c; cin.get(c); and convert it to the numeral with c -= '0'. But perhaps you can just read it as a string or use something like BigNum.

Matt Kane
cout? How about cin?
Tony
i'm just assuming but does c-= '0' refer to the value of c minus an ascii value of 0?
TopGunCpp
Also I think I will try inputing to a string and then con verting individually to integers, thanks for the tip :)
TopGunCpp
yeah I was thinking the same thing about the cin thing :P
TopGunCpp
I read the whole line as a string, and now I'm attempting to change each individual char to an int using atoi(). It keeps giving me this error "invalid conversion from ‘char’ to ‘const char*’" Do you have any idea why this might be and how I can fix it?
TopGunCpp
A: 

Off the top of my head this should fill up a character array which you can then iterate through. I realize it's not exactly what you were after but it's my preferred method.

void readfile(char *string)
{
    ifstream NumberFile; 
    NumberFile.open("./Number"); //For a unix file-system
    NumberFile >> string;
    NumberFile.close();
}

Also, to perform operations on the actual numbers you can use:

CharacterArray[ElementNumber] - '0'

and to get the number when it is small enough to fit in a datatype you add each element of the array multiplied by 10 to the power of its index.

njak32
+1  A: 

This could be done in a variety of ways, all of them boiling down to converting each char '0'..'9' to the corresponding integer 0..9. Here's how it can be done with a single function call:

#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
int main()
{
        std::string s = "12345678901234567900"; 
        std::vector<int> numbers;
        transform(s.begin(), s.end(), back_inserter(numbers),
                  std::bind2nd(std::minus<char>(), '0'));
        // output
        copy(numbers.begin(), numbers.end(),
             std::ostream_iterator<int>(std::cout, " "));
        std::cout << '\n';
}

When reading from a file, you could read the string and transform(), or even transform() directly from istream iterators, if there is nothing else in that file besides your number:

    std::ifstream f("test.txt");
    std::vector<int> numbers;
    transform(std::istream_iterator<char>(f),
              std::istream_iterator<char>(),
              back_inserter(numbers),
              std::bind2nd(std::minus<char>(), '0'));
Cubbi
Wow that seems extremely complex but logical all at the same time. Why is it though that subtracting '0' make it a manipulatable number? is there something special about '0'?
TopGunCpp
@TopGunCpp: `'0' - '0'` is the integer `0` because they are the same. `'1' - '0'` is the integer `1` because '1' is exactly 1 position after '0' in ASCII, EBCDIC and, if they exist, other charsets. etc. If you don't want to rely on this arithmetic, you could always `transform(s.begin(), s.end(), back_inserter(numbers), boost::lexical_cast<int, char>);`
Cubbi
@TopGunCpp: it's a simple integer subtraction. `'0'` is equivalent to 48 (ASCII). Since you're reading characters (text), and you want numbers, the quickest conversion is `'1'-'0' -> 49-48=1`, `'9'-'0' -> 57-48=9`.
jweyrich
Wow I never realized that '' might have been the syntax to use the ASCII value of the character, but that makes so much sense. for the sake of learning as much as I can I will try your transform method and attempt it in different situations too. Thanks so much for your help!
TopGunCpp