在 C 编程语言中,如何在读入数组之前嗅探二进制文件中的记录数?
例如,在打开文件并将记录读入数组之前,如何更好地判断二进制文件中有多少条记录?
MyFile = fopen("DATA.dat", "rb");
i = 0;
while (feof(MyFile) == 0) {
fread(&tempReadingRecord,sizeof(tempReadingRecord), 1, file);
if (feof(MyFile) == 0 {
i++;
}
}
fclose(MyFile);
}
printf("%d", i); /* does work to find out how many records but optimal? */
How do I tell in a better way how many records there are in a binary file before I open up the file and read the records into an array for example?
MyFile = fopen("DATA.dat", "rb");
i = 0;
while (feof(MyFile) == 0) {
fread(&tempReadingRecord,sizeof(tempReadingRecord), 1, file);
if (feof(MyFile) == 0 {
i++;
}
}
fclose(MyFile);
}
printf("%d", i); /* does work to find out how many records but optimal? */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
鉴于您显然正在处理固定大小的整个记录文件,您可以查找文件末尾,获取当前位置,然后除以记录大小。理论上,这存在可移植性问题(二进制文件可以包含附加到其末尾的任意数量的 NUL),但实际上,您不会在大多数常见系统上遇到问题(AFAIK,这种情况发生过)主要是在 CP/M 上,它没有显式存储文件长度,只存储块数,因此文件末尾总是被填充以填充最后一个块)。
顺便说一句,你的读取记录的循环有一个问题——事实上,几乎任何类型的循环 while (!feof(file)) 或任何类似的循环几乎都肯定会错误地工作(通常是它)我会“读”最后一条记录两次)。
Given that you're apparently dealing with an entire file of records of fixed size, you can seek to the end of the file, get the current position, and divide by the record size. In theory, this has a portability problem (a binary file can contain an arbitrary number of NULs appended to its end) but in practice you won't run into problems on most common systems (AFAIK, that happened primarily on CP/M, which didn't explicitly store a file length, just a number of blocks, so the end of the file was always padded to fill the last block).
BTW, your loop for reading records has a problem -- in fact, almost any loop of the sort
while (!feof(file))
, or anything similar is virtually guaranteed to work incorrectly (typically it'll "read" the last record twice).如果您正在处理仅包含固定大小记录的整个文件(似乎是这种情况),则可以使用 stat 来获取文件的总大小。 stat 从文件系统本身获取此信息,因此,您不需要提前打开文件。在我的 Linux 机器上,这个程序可以解决这个问题:
上面的程序打印二进制 ls 的总大小。一旦获得文件的总大小,您所需要做的就是将其除以表示记录的结构的大小。这将为您提供文件中的记录总数。
If you are dealing with an entire file only containing records of fixed size (which seems to be the case), you can use stat to get the total size of the file. stat gets this information from the file system itself and, therefore, you don't need to open the file in advance. On my linux box, this program does the trick:
The program above prints the total size of the binary ls. Once you have the total size of the file, all you need to do is to divide it by the size of the struct representing the records. This will give you the total number of records in the file.
它们是什么样的记录?
如果它们具有固定长度,则获取文件大小,并将其除以记录大小。
What kind of records are they?
If they are of a fixed length, take the file size, and divide it by the record size.
如果它们是固定大小的,请使用杰里的解决方案,否则按原样读取它们是我能想到的唯一方法。顺便说一句,是否会出现您会/不会根据对象数量读取文件的情况?
If they are fixed size, go with Jerry's solution, otherwise reading em in as you are is about the only way I could think of. BTW, is there going to be a case where you will/won't read the file in based on the number of objects?