Last active
December 1, 2021 18:05
-
-
Save VictorieeMan/b13af9f6ea45dc14c19a176ea8b7bb6b to your computer and use it in GitHub Desktop.
C++ For saving file lines to a vector<string>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C++ For saving file lines to a vector<string> | |
#pragma once | |
#include <filesystem> | |
#include <fstream> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
//Given a file path, read file line by line to StringVector and return it | |
std::vector<std::string> readFileToStringVector(std::string inputFilePath) { | |
std::ifstream inputFile; | |
std::vector<std::string> fileLines; | |
std::string line; | |
inputFile.open(inputFilePath, std::ios::in); | |
if (inputFile.is_open()) { | |
while (!inputFile.eof()) { | |
std::getline(inputFile, line); | |
fileLines.push_back(line); | |
} | |
} | |
else { | |
std::cout << "File not open..." << endl; | |
//Uncomment if you don't want program to exit immediatly after "File not open..." | |
//int dummy; | |
//std::cin >> dummy; | |
} | |
return fileLines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment