i unable store string array, when output b[c] nothing appears whatsoever, how can store array?
int main(int argc, char *argv[]) { string b[80000]; int c=0; string s; ifstream file(argv[1]); while(file >> s) { b[c]=s; c++; cout<<b[c]; } system("pause"); return 0; }
you're printing empty strings. move cout << b[c]; before c++;
i'd suggest use std::vector, avoid unnecessary temporary variables , magic constants:
#include <iostream> #include <vector> #include <string> #include <fstream> int main(int argc, const char* argv[]) { std::ifstream fin(argv[1]); std::vector<std::string> v { std::istream_iterator<std::string>(fin), std::istream_iterator<std::string>() }; for(const auto& elem: v) std::cout << elem << std::endl; return 0; } don't forget handle cases when file name isn't passed, or file doesn't exist.
Comments
Post a Comment