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

Threaded View

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

    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: 1116
Size:  27.8 KB

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

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
  •  





Click Here to Expand Forum to Full Width

Featured