使用 noskipws 读取文件?
File reading with noskipws?
我读取.txt
文件(包括空格和换行符 (((的方法是否正确? 我编写程序的指令要求我检测空格和换行符,以便可以操作它们。
char word_from_file;
ifstream input_file;
input_file.open (*recieved_file_name+".txt");
if (input_file.good() && input_file.is_open())
{
while (!input_file.eof())
{
input_file >> noskipws >> word_from_file;
if (*recieved_choice==1)
{
cout << *recieved_key;
encrypt (recieved_file_name, &word_from_file, recieved_key);
}
}
input_file.close();
}
您的代码是正确的,因为它读取空格和换行符,但输入的错误检查位置不正确,这可以通过使用istream::get()
来缩短。
char word_from_file;
while (input_file.get(word_from_file)) {
if (*recieved_choice == 1) {
cout << *recieved_key;
encrypt (recieved_file_name, &word_from_file, recieved_key);
}
}
istream::get()
从流中读取未格式化的字符,因此它将自动读取空格和换行符。
也无需检查文件是否打开或手动关闭它。该文件将在创建其范围结束时自动关闭,如果文件未打开,则任何尝试的输入操作都是无操作。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量