如何在c++中的路径字符串中添加反斜杠,以便ifstream函数正常工作而不会出现任何错误

问题描述 投票:0回答:2

在我的 C++ 脚本中,我通过文件收集路径并将其存储在字符串中。

我想通过

ifstream
使用路径文件,但路径只有一个反斜杠,例如:

path\dir1\dir2\file.cfg

我需要转换此路径,添加另一个反斜杠,以便它在

ifstream
内工作,例如

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

如何让我的 C++ 脚本以这种方式转换?

编辑

这个

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

ifstream DirLuaExec(path);

导致此错误:

||=== 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) ===|

但是这个:

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

ifstream DirExec(path);

这非常有效,这就是为什么我需要双反斜杠!!!

c++ file parameters ifstream backslash
2个回答
2
投票

使用 Raw String Literals 可以避免在字符串中添加 \ 的问题。它可用于 c++11。

#include<iostream>
#include<fstream>
#include<string>

int main () 
{
    std::string path {R"(path\dir1\dir2\file.cfg)"};
    std::ifstream  DirLuaExec(path);
}

1
投票
// 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
}

编译器仅在编译时读取字符串,在这种情况下,您将需要两个,因为第一个反斜杠将是转义字符。因此,如果您要在代码中使用静态路径字符串,则必须执行以下操作:

© www.soinside.com 2019 - 2024. All rights reserved.