I'm running the following program
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { ifstream input_file(argv[1]); vector<string> words; string line; while(getline(input_file, line)) { cout << line << endl; words.push_back(line); } input_file.close(); cout << "First and last word: " << words[0] << " " << words.back() << endl; return 0; } using the following text file as an input
permission copper operation cop rationale rest and I get the following output in terminal
permission copper operation cop rationale rest rest and last word: permission Why is the last word words.back() printed at the beginning of the line while erasing some of the text?
ifstream input_file(argv[1]);-- Basically, you told a fib to the file I/O library and stated the file is a text file, when it isn't a text file. Once you did that, expect all sorts of weird things to happen.