Unable to read .txt file using Command Line in c++ -
i trying read .txt file looks "-f filename.txt". project working on requires me name of file after "-f", open file name , extract information vector string.
i able except extraction. reason won't open file.
here code:
int main(int argc, char* argv[]) { ifstream file; string line; vector <string> lines; (int i=0; i<argc; i++) { if (strcmp( argv[1], "-f" )==0) { cout << "i here" << endl; std::string argv2 = argv[2]; cout << argv2 << endl; file.open(argv2.c_str()); if(!file.is_open()) { cout << "file failed open" << endl; } while (getline(file, line)) { lines.push_back(line); } } } (unsigned int j=0; j<lines.size(); j++) { cout << lines[j] << endl; }
my output
i here test.txt file failed open
x3
i don't because file.open(argv2.c_str());
worked when put string filename
instead of argv2
both strings... right?
the -f test.txt file under project folder , test.txt under debug folder. added -f test.txt in program arguments.
if run ide visual studio default current directory when running not executable file is, project root directory.
either change arguments -f debug\test.txt
, or move file project root directory.
Comments
Post a Comment