char array in C++ -
in c++ can read file contains integers , store in char array[] ?
yes, possible store numbers file in char array:
#include <fstream> #include <iterator> #include <algorithm> int main(int argc, char* argv[]) { std::ifstream in("input.txt"); char arr[100]; char* end = std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(), arr); return 0; } there 2 issues here. one, must know @ compile time size of array. two, each of input numbers must fit char. note formatted input, valid range not 0-255 or 0-127. it's valid character, '0', '2', 'a', 'c' , on valid inputs each character.
maybe want read file std::vector<std::string>?
#include <fstream> #include <iterator> #include <algorithm> #include <vector> #include <string> int main(int argc, char* argv[]) { std::ifstream in("input.txt"); std::vector<std::string> vec; std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(), std::back_inserter(vec)); std::transfrom(vec.begin(), vec.end(), vec.begin(), my_transform()); return 0; } here read numbers std::vector<std::string>. then, manipulate each string representation of number through my_transform functor. define functor simple struct defines std::string operator()(const std::string&). function-call operator takes number , expected return manipulation of number, wish change number.
Comments
Post a Comment