fin.ignore()跳过文件中的线
fin.ignore() skipping lines in file?
我正在处理一些代码,这些代码从文件中读取信息并将其存储在结构中。它正在处理我丢弃的所有文件,除一个文件外,其中有很多不同的错误。当文件中存在错误时,它会跳过跟随它的行,我不确定为什么。我的代码如下:
void readFile(char fileName[], accessRecord file[])
{
ifstream fin(fileName);
int i = 0;
while (fin.good())
{
fin >> file[i].fileName >> file[i].userName
>> file[i].timeStamp;
i++;
if (fin.fail())
{
fin.clear();
fin.ignore(256, 'n');
}
}
fin.close();
}
这是引起问题的文件。
问题是您不会在失败上消耗newline chracter。
为什么不作为字符串解析整行,然后验证它呢?这样,如果验证失败,您将安全地转到下一行。
#include <sstream>
#include <string>
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
if (!(iss >> file[i].fileName >> file[i].userName >> file[i].timeStamp)) {
// Error. go to the next line here
continue;
}
// process your data
}
ps:受读线读取文件的启发。而且,为什么不在普通C样式数组上使用std::vector
?考虑一下!
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 在不使用Ignore()读取二进制文件(C++)时忽略N个字节
- fin.ignore()跳过文件中的线