我对c ++和这个网站都很陌生,所以请耐心等待:)
我正在编写一个程序,您可以在其中输入文本并输出带有该文本的文本文件。
我现在拥有的是:
int _tmain(int argc, _TCHAR* argv[])
{
String^ fileName = "registry.txt";
String^ out;
StreamWriter^ sw = gcnew StreamWriter(fileName);
out = "hi";
out = out + "\n how you doing?";
sw->WriteLine(out);
sw->Close();
}
基本上我想要的是:
hi
how you doing?
但我得到的是:
hi how you doing?
有什么建议吗?
答案 0 :(得分:2)
使用静态数据成员Environment::NewLine
例如
out = out + Environment::NewLine + " how you doing?";
或者您可以明确指定转义符号'\ r'以及Windows中使用的'\ n'来分隔行。
out = out + "\r\n how you doing?";
以下是使用这两种方法的示例
#include "stdafx.h"
using namespace System;
using namespace System::IO;
int main(array<System::String ^> ^args)
{
String ^fileName( "Data.txt" );
String^ out;
StreamWriter^ sw = gcnew StreamWriter( fileName );
out = "hi";
out = out + "\r\n how you doing?";
sw->WriteLine(out);
out = "hi";
out = out + Environment::NewLine + " how you doing?";
sw->WriteLine(out);
sw->Close();
return 0;
}
输出
hi
how you doing?
hi
how you doing?