Hot Licks
Hot Licks

Reputation: 47739

fread oddity -- only reading a small number of items

In the middle of a rather large command line app that has been working. Made some changes to handle files larger than I can buffer and encountering a strange result from 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));

This results in the following output:

<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

(The file in question is 225008K in size.)

So how can I read only 448 samples of 8 bytes each and move from position 512 to position 58560512? Why isn't the requested amount of data being read?

Upvotes: 1

Views: 368

Answers (1)

jscs
jscs

Reputation: 64012

The return value of fread() should be the same as the nitems argument (numItemsToRead, in your case). The fact that items and numItemsToRead are different indicates that something went wrong with the read, but since the file pointer has advanced past what has been read, maybe there's something wrong with your buffer?

You should check errno and/or ferror() to see if there's any relevant information.

Upvotes: 2

Related Questions