Improve fileRead
This commit is contained in:
parent
4c11df3160
commit
8f9f4d461d
@ -39,7 +39,7 @@ std::string input(const char *message) {
|
||||
|
||||
bool fileExists(const char *fileName) { return access(fileName, 0) == 0; }
|
||||
|
||||
std::string fileRead(const char *filePath) {
|
||||
std::string fileRead(const std::string &filePath) {
|
||||
std::ifstream file(filePath, std::ios::binary);
|
||||
if (!file.is_open())
|
||||
throw std::runtime_error("File couldn't be found");
|
||||
@ -51,7 +51,10 @@ std::string fileRead(const char *filePath) {
|
||||
content.append(buf, file.gcount());
|
||||
content.append(buf, file.gcount());
|
||||
|
||||
file.close();
|
||||
if (!file.eof() && file.fail())
|
||||
throw std::runtime_error("Error: Failed to read the file '" + filePath +
|
||||
"'");
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
@ -167,7 +170,7 @@ int main() {
|
||||
}
|
||||
std::string fileContents;
|
||||
try {
|
||||
fileContents = fileRead(inputFile.c_str());
|
||||
fileContents = fileRead(inputFile);
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "Failed to read file: " << e.what() << std::endl;
|
||||
return 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user