1 Attachment(s)
Why does this simple file read method does not work?
Code:
std::string Program::readFile(const char* path)
{
//Variables.
std::ifstream file;
std::string content;
//Set exception mask for file stream.
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
//Try to read the file.
try
{
//Open the file.
file.open("test.txt");
//Read the content.
file >> content;
//Close the file.
file.close();
//Return the content.
return content;
}
//Something went wrong.
catch (std::ifstream::failure e)
{
std::cout << "[Read Error]: " << path << " => " << e.what() << std::endl;
}
return NULL;
}
If I run this method I get this access violation exception (Is being thrown at the file.open() call):
Code:
Exception thrown at 0x00007FFD4C42FCB1 (ucrtbased.dll) in LearnOpenGL.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
The exception is in the file xstring at these lines:
Code:
#else // _HAS_CXX17
return _CSTD strlen(reinterpret_cast<const char*>(_First));
#endif // _HAS_CXX17
Attachment 35897
Tnank you!
Re: Why does this simple file read method does not work?
The catch clause should be by ref:
Code:
catch (std::ifstream::failure& e)
Re: Why does this simple file read method does not work?
Quote:
Originally Posted by
2kaud
The catch clause should be by ref:
Code:
catch (std::ifstream::failure& e)
I did it but the issue was not fixed...
By the way if I remove the line:
Code:
//Set exception mask for file stream.
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
then this access violation is not been thrown.
Re: Why does this simple file read method does not work?
Instead of:
try:
You are trying to assign address NULL (0) to a string!
Re: Why does this simple file read method does not work?
Quote:
By the way if I remove the line:
Code:
//Set exception mask for file stream.
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
then this access violation is not been thrown.
Yes. An access violation for fail/bad is only thrown if so configured. Otherwise you need to test the status bits. By default, if the open didn't work then the status wouldn't be good and so the file extraction would fail - but an exception wouldn't be thrown. In this case you would need to test. there is .is_open() to see if a file opened ok. and .good() to see if the file stream is OK after an operation. If you use the file stream name where a bool is expected, then true is returned if the file stream is good and false if it is not.
Re: Why does this simple file read method does not work?
Quote:
Originally Posted by
2kaud
Instead of:
try:
You are trying to assign address NULL (0) to a string!
Haha, I just figured it out! Yeah, that was the problem! I was trying to std::cout << returned_string << std::endl . I thought that the result would display zero but apparently it does not work
this way...
I believe the reason is that the << operator requires an actual object right? It seems quite difficult to forget C habits...
Re: Why does this simple file read method does not work?
Quote:
I believe the reason is that the << operator requires an actual object right? It seems quite difficult to forget C habits...
When I saw NULL I wondered if you had programmed in C. Unless using C-type functions, NULL isn't used in C++. nullptr is used to indicate that a pointer is 'null' - not NULL. But 'raw pointers' shouldn't really be used - in C++ there are 'managed pointers' :)