用户输入字符串的文件附加问题..C++
File appending problem with users input with strings... C++
我正在使用用户输入将字符串存储在 txt 文件中!但是我有一个问题。虽然输入用户字符串进行文件写入很好,但我想添加新行,但是当用户为文件输入短语时,它会将输入写入文件,但不在一个完整的行中,对于每个单词它需要一行......
法典:
int Char;
std::string Input;
std::ofstream ofDocument;
std::ifstream ifDocument;
ifDocument.open("Path\Document.txt");
ofDocument.open("Path\Document.txt", std::fstream::app); //I tried ios::app but same result...
if (ifDocument)
{
while (1)
{
std::cin >> Input;
ofDocument << Input << std::endl;
switch (Char = _getch())
{
case '@':
std::cout << "You pressed '@', exiting application..." << "n" << std::endl;
exit(0);
}
}
}
else
{
std::cout << "Cannot create 'Document.txt'. Please retry..." << std::endl;
ThisFunction();
}
示例:用户输入:第一行:您好,我在文本文件中编写
第二行:这行得通吗?
文件中的输出: 您好 : 新行 i : 新行 上午 : 换行符 书写:换行 在 : 新行 A : 新行 文本 : 换行符 文件 : 新行 将:新行 这 : 新行 工作?
任何帮助表示赞赏!
您应该尝试将 getline(std::cin, Input( 放在 std::cin>> Input 下方以捕获所有空格,这样它就不会在每个单词中都是一行。希望这有效
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题