简体   繁体   English

从 C++ 中的变量打印到文件的路径

[英]Printing a path to a file from a variable in C++

Lets say I have this function:假设我有这个功能:

void printPathToFile( std::wstring myPath ){
    std::wstringstream ss;
    ss << myPath;
    //When printing myPath as soon as there is a \ it stops so this information is lost.
    ss << getOtherReleventLogingInformation();

    std::wofstream myfile;
    myfile.open ("C:\\log.txt", std::wofstream::app|std::wofstream::out);
    myfile  << ss.str();
    myfile.close();
}

I do not control the myPath argument.我不控制myPath参数。 Right now it doesn't have \\\\ in the path name so the stream interprets them as escape sequence which is not what I want.现在它的路径名中没有\\\\ ,因此流将它们解释为转义序列,这不是我想要的。

How can I use a std::wstring variable as a raw string?如何使用 std::wstring 变量作为原始字符串?

If it was a string literal I could use R"C:\\myPath\\" but how do I achieve the same thing without a string literal?如果它是一个字符串文字,我可以使用R"C:\\myPath\\"但是我如何在没有字符串文字的情况下实现同样的目标?

A possible approach could be to loop through the path name and add an extra backslash where needed but surely c++ has something more robust and elegant..?一种可能的方法是遍历路径名并在需要的地方添加一个额外的反斜杠,但 C++ 肯定有更健壮和优雅的东西..?

EDIT :编辑

My problem was misdiagnosed.我的问题被误诊了。 Turns out the backslashes don't cause any trouble what I had to add was:结果反斜杠不会引起任何麻烦,我必须添加的是:

#include <codecvt>
#include <locale>

const std::locale utf8_locale
        = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>());
myFile.imbue(utf8_locale);

As explained here: Windows Unicode C++ Stream Output Failure如此处所述: Windows Unicode C++ Stream Output Failure

The file path now displays correctly, I thought using a wofstream took care of the locals for you so the non-ANSII characters would display correctly.文件路径现在正确显示,我认为使用wofstream为您处理本地文件,因此非 ANSII 字符将正确显示。

I would propose you simply replace \\\\ by / , they work the same (even better because they are valid on all platform):我建议您只需将\\\\替换为/ ,它们的工作方式相同(甚至更好,因为它们在所有平台上都有效):

void printPathToFile( std::wstring myPath )
{
    std::wstring mySafePath = myPath;
    std::replace( mySafePath.begin(), mySafePath.end(), '\\', '/');

    // then use mySafePath in the rest of the function....
}

It does: boost filesystem.它确实:提升文件系统。 You use a path to pass, well, paths, around instead of strings.您使用path来传递路径,而不是字符串。 Here's the hello world for boost filesystem:这是boost文件系统的hello world:

int main(int argc, char* argv[])
{
  path p (argv[1]);   // p reads clearer than argv[1] in the following code

  if (exists(p))    // does p actually exist?
  {
    if (is_regular_file(p))        // is p a regular file?   
      cout << p << " size is " << file_size(p) << '\n';

    else if (is_directory(p))      // is p a directory?
      cout << p << "is a directory\n";

    else
      cout << p << "exists, but is neither a regular file nor a       directory\n";
  }
  else
    cout << p << "does not exist\n";

  return 0;
}

http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/tutorial.html http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/tutorial.html

Edit: note also that this library is being considered for addition to the standard, and can currently be used from the std::experimental namespace, depending on your compiler/standard library version:http://en.cppreference.com/w/cpp/header/experimental/filesystem编辑:另请注意,该库正在考虑添加到标准中,目前可以从 std::experimental 命名空间使用,具体取决于您的编译器/标准库版本:http ://en.cppreference.com/w/ cpp/头文件/实验/文件系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM