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; }
|
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);
|
std::ifstream file(filePath, std::ios::binary);
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
throw std::runtime_error("File couldn't be found");
|
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());
|
||||||
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;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +170,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
std::string fileContents;
|
std::string fileContents;
|
||||||
try {
|
try {
|
||||||
fileContents = fileRead(inputFile.c_str());
|
fileContents = fileRead(inputFile);
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
std::cerr << "Failed to read file: " << e.what() << std::endl;
|
std::cerr << "Failed to read file: " << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user