How to Get Error Message When Ifstream Open Fails

How to get error message when ifstream open fails

Every system call that fails update the errno value.

Thus, you can have more information about what happens when a ifstream open fails by using something like :

cerr << "Error: " << strerror(errno);

However, since every system call updates the global errno value, you may have issues in a multithreaded application, if another system call triggers an error between the execution of the f.open and use of errno.

On system with POSIX standard:

errno is thread-local; setting it in one thread does not affect its
value in any other thread.


Edit (thanks to Arne Mertz and other people in the comments):

e.what() seemed at first to be a more C++-idiomatically correct way of implementing this, however the string returned by this function is implementation-dependant and (at least in G++'s libstdc++) this string has no useful information about the reason behind the error...

Get std::fstream failure error messages and/or exceptions

From checking it out I found that also errno and also GetLastError() do set the last error and checking them is quite helpful. For getting the string message use:

strerror(errno);

ifstream failing to open

As from discussed came to know the problem is relative path.

fstream support relative paths as below..

Consider the following case where your input file is one level up than exe file.

E:\MyProgramBin\YourExe.exe
E:\YourInputFile.ppm

In this case, you can create your filename as below.

filename1 = "..\YourInputFile.ppm"

and use that filename1 in ifstream

std::ifstream src(filename1, std::ios_base::binary);

C++ ifstream Error Checking

paramFile >> tmp; If the line contains spaces, this will not read the whole line. If you want that use std::getline(paramFile, tmp); which reads up until the newline. Basic error checking is done by examining the return values. For example:

if(paramFile>>tmp) // or if(std::getline(paramFile, tmp))
{
std::cout << "Successful!";
}
else
{
std::cout << "fail";
}

operator>> and std::getline both return a reference to the stream. The stream evaluates to a boolean value which you can check after the read operation. The above example will only evaluate to true if the read was successful.

Here is an example of how I might make your code:

ifstream paramFile("somefile.txt"); // Use the constructor rather than `open`
if (paramFile) // Verify that the file was open successfully
{
string tmp; // Construct a string to hold the line
while(std::getline(paramFile, tmp)) // Read file line by line
{
// Read was successful so do something with the line
}
}
else
{
cerr << "File could not be opened!\n"; // Report error
cerr << "Error code: " << strerror(errno); // Get some info as to why
}

fstream fails to open file

Okay, apparently a reboot was all it takes. Today everything works flawlessly. No permissions, file or line of code was changed. I assume either something went terribly wrong, when my Laptop came back from hibernation. Or it was some pending ubuntu update? I really have no idea. I could swear it was shutdown at least once the past 2-3 days. But jeah, I have no other explanation. Maybe someone smarter can answer this with his glass ball...

Anyway, thanks for the answers, appreciate it! At least I learned about strace $:^)



Related Topics



Leave a reply



Submit