使用ofstream,c ++将引号写入文本文件

时间:2010-11-18 02:12:40

标签: c++ ofstream

我想写出文件中的引号,我不知道语法应该如何。

ofstream file("myfile.txt");
    if ( file.is_open())
    {
        file << "\n";
        file << ""type of file""<< "=" << '\n'; // obviously this is wrong
        file << "name = \n";
    }

我希望文本文件如下所示:

  

“文件类型”=

     

name =

我该怎么做?

4 个答案:

答案 0 :(得分:3)

你应该能够"\一起逃离\"

file << "\"type of file\""<< "=" << '\n';

答案 1 :(得分:3)

使用\"代替",即:

file << "\n"; // note not "/n"
file << "\"type of file\"" << "=" << "\n";
file << "name = \n";

当然中间线可能只是:

file << "\"type of file\"=\n";

答案 2 :(得分:1)

使用转义字符

file << "\"type of file\"" << "=" << "\n";

答案 3 :(得分:0)

if (ofstream file("myfile.txt")) 
{ 
    file << "/n" 
            "\"type of file\"=\n"
            "name = \n"; 
}