我可以让文件保存正确但我似乎无法获得多个单词来写入.txt,就像我输入“Hi purple”它只写“Hi”,这里是代码
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
system("color F0");
string name0;
cout << "Please enter a file name, no spaces/special characters" << endl;
cin >> name0;
name0 = name0+".txt";
system("cls");
cout << " FISHSOFT" << endl;
cout << "The best text editor in the world" << endl << endl;
string text;
cin >> text;
ofstream myfile;
myfile.open (name0.c_str() , ios::out | ios::trunc);
myfile << text;
myfile.close();
system("PAUSE");
return 0;
}
答案 0 :(得分:3)
使用std::getline
读取整行输入,包括空格。
答案 1 :(得分:2)
std :: cin能够一次获得几个参数。
这意味着你可以写:
zcat 60GB.csv.gz |awk 'NR%43000000==1{x="part-"++i".csv";}{print > x}'
默认情况下,空格是参数之间的分隔符。
要避免此行为,您可以使用getLine:
std::cin >> name0 >> name1 >> name2;
// input: a1 a2 a3
// make: name0: a1, name1: a2, name3:a3
答案 2 :(得分:2)
cin >> text;
将从输入流中读取一个以空格分隔的标记。一个词在==一个单词出来。
std::getline(cin, text);
会读完整行。阅读更多是很棘手的,但通常是围绕多次调用getline的循环。
建议:节省时间并启动IDE的调试器,以便在发布问题之前查看代码中发生的情况。几乎总是更快,如果没有,你可以做出更好,更紧凑的问题。
答案 3 :(得分:1)
有两种方法可以获得带空格和特殊字符的字符串。
希望这符合您的目的。