Search code examples
c++fileparametersifstreambackslash

how to add backslashes to a path string in c++ so that the ifstream function works correctly without any errors


in my C++ script, I am collecting a path through a file and storing it in a string.

I want to work with the path file through ifstream but the path comes with only one backslash, example:

path\dir1\dir2\file.cfg

I need to convert this path, adding another backslash so that it works within the ifstream, example:

path\\dir1\\dir2\\file.cfg

How do I get my C++ script to convert this way?

Edit

THIS

string path = "path\dir1\dir2\file.cfg";

ifstream DirLuaExec(path);

CAUSE THIS ERROR:

||=== Build: Debug in BatchParamCollect (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int main()':|
main.cpp|19|error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)'|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|495|note: candidate: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|495|note:   no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*'|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|481|note: candidate: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|481|note:   candidate expects 0 arguments, 1 provided|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|455|note: candidate: std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)|
C:\Program Files (x86)\CodeBlocks\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\fstream|455|note:   no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const std::basic_ifstream<char>&'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in BatchParamCollect (compiler: GNU GCC Compiler) ===|
||=== Run: Debug in BatchParamCollect (compiler: GNU GCC Compiler) ===|

BUT THIS:

string path = "path\\dir1\\dir2\\file.cfg";

ifstream DirExec(path);

THIS WORKS PERFECTLY, THAT'S WHY I NEED DOUBLE BACKSLASHES!!!


Solution

  • // Example program
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string path = "path\\dir1\\dir2\\file.cfg";
        string new_path = "";
        for(int i = 0; i < path.size(); i++)
        {
            if(path[i] == '\\')
            {
                new_path += path[i];   
                new_path += path[i];   
            }
            else
            {
                new_path += path[i];     
            }
        }
        
        cout << "Old path = " << path << endl; // This prints out path\dir1\dir2\file.cfg
        cout << "New path = " << new_path << endl; // This prints out path\\dir1\\dir2\\file.cfg
    }
    

    The compiler only reads string when you compile, and in that case you will need two as the first back slash will be an escape character. So if you were to have a static path string in code you would have to do something like this: