使用 getline 仅读取换行符或文本
Using getline to read either just a newline or text
所以我需要做的是从用户那里获取输入,特别是文件名,用户只需按 Enter 默认为某个文件名。这是我所拥有的:
cout << "Where should I save the exam (default exam.txt): ";
getline(cin, examfilename);
if (examfilename == "") {
examfilename = "exam.txt";
}
cout << "Where should I save the key (default key.txt): ";
getline(cin, keyfilename);
if (keyfilename == "") {
keyfilename = "key.txt";
}
运行时,输出全部Where should I save the exam (default exam.txt): Where should I save the key (default key.txt):
在一行上,末尾有一个闪烁的光标。
如果用户按 Enter 键,我如何读取文件名,但也使用默认值?
您可以像这样在控制台中手动引入换行符:
if (examfilename == "") {
examfilename = "exam.txt";
cout << endl;
}
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 在 Stream C++ 文本之前有一个额外的换行符
- 流:CSV 文件中的换行符
- 如何在三元条件运算符中添加换行符和连接? :在 C++ 中
- 正则表达式 获取两个换行符之间的文本
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- C++是否可以将带有换行符的文本传递给C++程序
- 读取整数对,直到文本输入文件中的换行符
- 正在读取由换行符分隔的文本文件.unix上的C++
- 如何在不添加换行符的情况下将文本附加到QPlainTextEdit,并保持滚动在底部
- 输出到文本文件时C++换行符
- QListWidget,插入换行符以获得较长的文本
- 检测富编辑文本控件中的换行符
- 使用 getline 仅读取换行符或文本
- 如何在 XML 文本(在标记内)中搜索换行符
- 为什么 fstream::tellg() 返回值会因输入文本文件中的换行符数而增大,当文件针对 Windows (r
- 如何在写入文本文件时向一行添加换行符
- 如何从包含空格和换行符的文本文件中提取特定数据
- 查找由换行符分隔的字符串中的特定文本