Character by Character Input from a file, in C++ -
is there way input file 1 number @ time? example want store following integer in vector of integers since long , can't held long long int.
12345678901234567900
so how can read number file can:
vector<int> numbers; number.push_back(/*>>number goes here<<*/)
i know above code isn't complete hope explains trying do. i've tried google , far has proved innefective because tutorials c coming aren't helping me much.
thank advance, dan chevalier
this done in variety of ways, of them boiling down converting each char '0'..'9' corresponding integer 0..9. here's how can done 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 file, read string , transform(), or transform() directly istream iterators, if there nothing else in file besides 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'));
Comments
Post a Comment