Answers for "how to open and read text files in c++"

C++
2

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");
}
Posted by: Guest on April-29-2022

Code answers related to "how to open and read text files in c++"

Browse Popular Code Answers by Language