如何在 c++ 中创建文本编辑器不使用任何图形库?
我首先声明一个字符串并将用户输入的所有文本存储在其中。然后我转移到一个文件。我不知道如何在输入中添加换行符。我只是一个初学者..
示例代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string x;
string y;
ofstream a_file("example.txt");
getline ( cin , x);
a_file<<x;
a_file<<y;
}
I started out by declaring a string and storing all the text entered by the user in it. Then I transfer to a file. I cant figure out how add a line break to the input. I am just a beginner..
example code :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string x;
string y;
ofstream a_file("example.txt");
getline ( cin , x);
a_file<<x;
a_file<<y;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要向输出添加换行符,您需要向其写入字符串
"\n"
。就这么简单。您还可以将最后两个语句合并为一个:
但是如果您想将换行符添加到字符串中,而不仅仅是文件中,您可以这样做:
To add a line break to the output, you need to write the string
"\n"
to it.It's as simple as that. You can also combine the last two statements into one:
But if you want to add the line break to the string, and not only to the file, you can do this: