c++ - How to read txt file into vector and cout the file? -
#include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { vector<int> temp; ifstream infile; infile.open("numbers"); if (infile.fail()) { cout << "could not open file numbers." << "\n"; return 1; } int data; infile >> data; while (!infile.eof()) { temp.push_back(data); infile >> data; } cout << data << " " << endl; }
i trying cout numbers text file "numbers" using vector.
15 10 32 24 50 60 25
my experience pretty nil, , guidance on why fails open helpful.
your code fine you're printing wrong thing. change bottom of main this
int data; while (infile >> data) { temp.push_back(data); } for( vector<int>::iterator = temp.begin(); != temp.end(); i++) { cout << *i << endl; }
*edited after reading suggested dup.
Comments
Post a Comment