Skip to content

Instantly share code, notes, and snippets.

@VictorieeMan
Last active December 1, 2021 18:05
Show Gist options
  • Save VictorieeMan/b13af9f6ea45dc14c19a176ea8b7bb6b to your computer and use it in GitHub Desktop.
Save VictorieeMan/b13af9f6ea45dc14c19a176ea8b7bb6b to your computer and use it in GitHub Desktop.
C++ For saving file lines to a vector<string>
//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