Copy an entire file into a vector of string - C++ File Stream

C++ examples for File Stream:File Operation

Description

Copy an entire file into a vector of string

Demo Code

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {/*from   w  w  w  . j  a  v a2  s  .  c  o  m*/
   vector<string> v;
   ifstream in("main.cpp");
   string line;
   while(getline(in, line))
      v.push_back(line); // Add the line to the end
   for(int i = 0; i < v.size(); i++)
      cout << i << ": " << v[i] << endl;
}

Result


Related Tutorials