c++ - Error when attempting to input strings into dynamically allocated array -
i'm trying code following program read single word per line text document, print same words text document, reason encountering rather large error after adding in:
for(int = 0; < num_lines; i++) { getline (myfile_in, line); stringstream(line) >> arr[i]; }
im not sure why causing error because copied loop program wrote previously. error goes away once remove stringstream line, far i'm aware, need in order copy contents on array. appreciated :)
#include <fstream> #include <string> #include <sstream> #include <assert.h> using namespace std; int main(void) { ifstream myfile_in; myfile_in.open ("words_in.txt"); ofstream myfile_out; myfile_out.open ("words_out.txt"); string line; int num_lines = 0; string *arr; assert (!myfile_in.fail()); myfile_in >> line; while (!myfile_in.eof()) { getline (myfile_in, line); num_lines++; } arr = new string[num_lines]; for(int = 0; < num_lines; i++) { getline (myfile_in, line); stringstream(line) >> arr[i]; } for(int = 0; < num_lines; i++) { myfile_out << arr[i] << endl; } myfile_in.close(); myfile_out.close(); return 0; }
why not getline (myfile_in, arr[i]);
?
Comments
Post a Comment