C++:如何在标准上放置多行::字符串数据("here");

C++: How to put multiple lines on std::string data("here");

本文关键字:字符串 数据 here 标准 C++      更新时间:2023-10-16

我需要写一个。批处理文件;我如何插入多行?

参见:std::string data(");

#include <iostream>
#include <fstream>
#include <string> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int name;
std::cout << "Insert your name";
std::cin >> name;
const char *path="C:/Users/Public/Desktop/file.bat";
std::ofstream file(path);
std::string data( /* "FIRST LINE + ENTER + SECOND LINE + name + ENTER + THIRD LINE"*/ );
file << data;
}

Thanks in advance

使用n作为换行符。

std::string data("FIRST LINEnSECOND LINEnTHIRD LINE");

在c++ 11中,您还可以使用原始字符串文字:

    std::string data(
R"(line 1
line 2
line 3
...)"
    );