-
August 28th, 2020, 07:35 AM
#1
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
Tnank you!
Last edited by babaliaris; August 28th, 2020 at 08:30 AM.
-
August 28th, 2020, 09:45 AM
#2
Re: Why does this simple file read method does not work?
The catch clause should be by ref:
Code:
catch (std::ifstream::failure& e)
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 28th, 2020, 10:25 AM
#3
Re: Why does this simple file read method does not work?
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.
Last edited by babaliaris; August 28th, 2020 at 10:27 AM.
-
August 28th, 2020, 10:31 AM
#4
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 28th, 2020, 10:37 AM
#5
Re: Why does this simple file read method does not work?
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 28th, 2020, 10:39 AM
#6
Re: Why does this simple file read method does not work?
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...
-
August 28th, 2020, 10:43 AM
#7
Re: Why does this simple file read method does not work?
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'
Last edited by 2kaud; August 28th, 2020 at 10:56 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|