奇怪的怪 - 只读少量的物品

时间:2012-06-28 20:39:39

标签: objective-c xcode macos fread

在一个相当大的命令行应用程序中间工作。做了一些更改来处理大于我可以缓冲的文件并遇到来自fread的奇怪结果。

DebugLog(@"Reading %d samples of a total %d samples of size %d, leaving %d samples remaining", numSamplesToRead, numSamplesInFile, sizeof(short) * channels * scaleFactor, numSamplesRemainingInFile);
DebugLog(@"Position in file is %d", ftell(in));
items = fread(dataBuffer, sizeof(short) * channels * scaleFactor, numSamplesToRead, in);
DebugLog(@"%d samples read", items);
DebugLog(@"Position in file is %d", ftell(in));

这导致以下输出:

<main:(604)> Reading 29278208 samples of a total 115202048 samples of size 8, leaving 85923840 samples remaining
<main:(605)> Position in file is 512
<main:(607)> 448 samples read
<main:(608)> Position in file is 58560512

(有问题的文件大小为225008K。)

那么如何才能只读取448个8字节样本并从位置512移动到位置58560512?为什么没有读取所请求的数据量?

1 个答案:

答案 0 :(得分:2)

fread()的返回值应与nitems参数(numItemsToRead相同)(在您的情况下)。 itemsnumItemsToRead不同的事实表明读取出现了问题,但由于文件指针已超过已读取的内容,可能是您的缓冲区有问题?

您应该检查errno和/或ferror(),看看是否有相关信息。