Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to open and read text files in c++

// This is how you read a file, and keep it's text in a vector of strings
#include <bits/stdc++.h>
using namespace std;

vector<string> Read_File(string FileName){
    ifstream infile;
    infile.open(FileName);
    vector<string> empty;
    if(infile.fail()){
        cout << "File Could Not Be Reached";
        return empty;
    }
    vector<string> answer;
    while(infile.good()){
        string str;
        infile >> str;
        answer.push_back(str);
    }
    infile.close();
    return answer;
}
int main(){
  vector<string> file_text = Read_File("textfile.txt");
}
 
PREVIOUS NEXT
Tagged: #open #read #text #files
ADD COMMENT
Topic
Name
1+4 =