CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Why does this simple file read method does not work?

  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Question 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
    Name:  Capture.jpg
Views: 45
Size:  27.8 KB

    Tnank you!
    Last edited by babaliaris; August 28th, 2020 at 08:30 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,289

    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++17 Compiler: Microsoft VS2019 (16.7.7)

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Why does this simple file read method does not work?

    Quote Originally Posted by 2kaud View Post
    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,289

    Re: Why does this simple file read method does not work?

    Instead of:
    Code:
    return NULL;
    try:
    Code:
    return "";
    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++17 Compiler: Microsoft VS2019 (16.7.7)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,289

    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++17 Compiler: Microsoft VS2019 (16.7.7)

  6. #6
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Why does this simple file read method does not work?

    Quote Originally Posted by 2kaud View Post
    Instead of:
    Code:
    return NULL;
    try:
    Code:
    return "";
    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...

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,289

    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++17 Compiler: Microsoft VS2019 (16.7.7)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)