Search code examples
c++filefstreamostreamistream

C++: Read files that dont have an extension


I have some files with the type "File" I mean instead of "Resource\sample.txt", their name is "Resource\sample" Now I want to read them with c++ and store them in a string. Here is my code:

std::stringstream textBuffer;
std::fstream fileHandle;
std::string content
fileHandle.open("Resource\sample", std::ios::in);
textBuffer << fileHandle.rdbuf();
content = textBuffer.str();
fileHandle.close();

But when I compile this the value of "content" variable is equal to "". Help me on this . Thanks in Advance. Please note that the only problem is that the file has no extension such as .txt or .dat or .bin of whatsoever.


Solution

  • You did not properly escape the backslash in your filename string. You need to either escape the string:

    fileHandle.open("Resource\\sample", std::ios::in);
    

    or use a raw string literal:

    fileHandle.open(R"(Resource\sample)", std::ios::in);