Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problem with fread

Hello,


Since i install Lion i have a problem with fread.

I used xcode 3.2.6 and seeing this problem i desinstall xcode 3.2.6 and install the lastest xcode (4.2).

The problem remain.


Here is the code:


unsigned char *ptr;

size_t len;

FILE *file;


if (!(ptr = calloc(len, sizeof(char))))

{

// error

}

if (fread(ptr, sizeof(char), len, file) != len)

{

// error

}


the problem is :


After ptr is allocated (ok)

if len is > 2147483647 (int size) there is no error, the file is not read. (incredible but true)

else (len <= 2147483647) its ok (file is read).


len is always ok, even for 3Gb or more.


Any ideas ?


Thank you

Mac Pro, Mac OS X (10.7.2), xcode 4.2

Posted on Oct 19, 2011 12:54 PM

Reply
27 replies

Oct 28, 2011 11:37 AM in response to stéphane69

Here is an example of what your code should look like:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


int main(int argc, char *argv[])

{

FILE *fin = NULL, *fout = NULL;

char *ptr = NULL;

size_t len = 65536;

int result = -1;


if(!(fin = fopen(argv[1], "rb")))

{

printf("The input file: %s could not be opened\n", argv[1]);

return -1;

}

if((fout = fopen(argv[2], "rb")))

{

printf("The output file %s already exist\n", argv[2]);

fclose(fin);

return -1;

}

if (!(fout = fopen(argv[2],"wb")))

{

printf("Cannot write output file %s\n", argv[2]);

fclose(fin);

return -1;

}


if (!(ptr = malloc(len)))

{

printf("Canot allocate %zd bytes\n", len);

fclose(fin);

fclose(fout);

return -1;

}


while(1)

{

int amountRead = fread(ptr, sizeof(char), len, fin);


if(amountRead)

{

int amountWritten = fwrite(ptr, sizeof(char), amountRead, fout);


if(amountWritten != amountRead)

{

printf("Cannot write file\n");

break;

}

}


if(amountRead < len)

{

result = 0;

break;

}

}


fclose(fin);

fclose(fout);


free(ptr);



return result;

}

Nov 4, 2011 4:51 PM in response to etresoft

Team answer after reporting this bug.


Hello Stéphane,


This is a follow up to Bug ID# 10376104. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 6434977. The original bug number being used to track this duplicate issue can be found in the Related Problem section of your bug report's Problem Detail view.


Thank you for submitting this bug report. We truly appreciate your assistance in helping us discover and isolate bugs.


Best Regards,


Developer Bug Reporting Team

Apple Worldwide Developer Relations

Aug 3, 2012 5:00 AM in response to etresoft

"There is no reason to ever issue a single I/O request of 2 GB. There is no reason to issue a single I/O request of more than 64k."


I don't know, maybe here's one.


I have a 24 GB file that I need to transfer to Windows via USB drive.


I can't write > 4 GB to FAT32, I can't write at all to NTFS, and Windows won't read HFS+.


All I can think of is to split the file into smaller pieces and then put them back together under Windows with something like "copy /b Archive.zip_* Archive.zip". Is there an easy alternative?


It's only 12 parts of 2 GB each; a few more (378479) if they were only 64 kB each--I think I'll fread as much as I can.


Somewhat more related to this thread: I tried 4 GB which seems to run without error under Snow Leopard but the last partial read always comes out to 4 GB, so I am sticking with 2 GB chunks (2^31 - 1 to be exact) which gets the length of the last part correct.

Aug 3, 2012 5:40 AM in response to schecae1

yes.

I am in contact with apple and they work on this bug (my bug report is a duplicate bug as apple wrote) and will be fixed in future release, it seems they have problem with this bug because it is since Lion (and mountain Lion) (it works on Snow Leopard) and in some hardware ( not all machines ).

I had to modified some program to check INT_MAX and split in 2 GB parts since Lion.

Why apple put size_t as fread return value and not unsigned short (like the hard code the guy put in his program above) 🙂 ?

Aug 3, 2012 2:14 PM in response to schecae1

schecae1 wrote:


I have a 24 GB file that I need to transfer to Windows via USB drive.


I can't write > 4 GB to FAT32, I can't write at all to NTFS, and Windows won't read HFS+.


All I can think of is to split the file into smaller pieces and then put them back together under Windows with something like "copy /b Archive.zip_* Archive.zip". Is there an easy alternative?


Use the "split" command to split your file into chunks. Then copy each chunk. That "copy" command on Windows should suffice to restore all the parts into one big whole.


This thread is about a really low-level, ancient piece of code, being used horribly wrong. Unless you are really talking about programming, such a question would be better suited to the MacOS X Technologies forum. In al cases, it is best to start your own question instead of piggy-backing onto someone else's question. Dredging up an old thread like this is a sure way to get your question ignored.

Aug 3, 2012 3:05 PM in response to etresoft

> This thread is about a really low-level, ancient piece of code, being used horribly wrong.


It is proved in this thread , you really don't understand what is really about.

Learn programming first as i ever said to you 6 months ago and come back after.

Apple unlike you is perfectly aware of this bug and the code i used is perfectly correct.

So please stop bashing and trolling.

Thank you.

When i got some news about this bug i'll post it.

Aug 3, 2012 5:55 PM in response to stéphane69

stéphane69 wrote:


When i got some news about this bug i'll post it.

Don't hold your breath waiting. That's just boilerplate text that everyone gets when they file a bug. When and if any changes ever get made, they will be made against the original bug report, not your duplicate. You will get no further news from Apple about this.


As I have tried to explain, you shouldn't ever allocete a buffer anywhere close to 2 GB. In order for such a program to run, your machine would have to have 2 GB of free RAM. You are asking someone to swap out half their RAM just to make one function call.

Aug 4, 2012 12:50 AM in response to etresoft

Why you continue to talk about a problem you have no idea about ?


Apple contact me personnaly (not on bug report) about my duplicate bug to talk about the original bug (i was not the first to detect it) to explain the problems they have on it and why it takes 6 months.

Please talk about what you know.


Excuse me but you have no idea about this issue.


> you shouldn't ever allocete a buffer anywhere close to 2 GB


Again, please talk about what you understand and know.

Learn programming.

return value of fread is not unsigned short (your example above) or int and there is reason for that.

reasons that obviously you do not know.

Again learn programming.

And if you want to continue barking like you do on this problem its your choice, its just wasting time and trolling.


Have a nice week end.

Aug 4, 2012 6:49 AM in response to stéphane69

stéphane69 wrote:


Again, please talk about what you understand and know.

Learn programming.

return value of fread is not unsigned short (your example above) or int and there is reason for that.

reasons that obviously you do not know.

Again learn programming.

And if you want to continue barking like you do on this problem its your choice, its just wasting time and trolling.

At this point, it is mostly morbid fascination 🙂


I'm curious why you keep saying "unsigned short". Is that because I used a buffer size of 65536? It has nothing to do with unsigned short. That is just an optimal buffer size. Once you get any larger than that, the return on speed from larger sizes drops very quickly. Furthermore, in a modern application, your process will lock up if you try a buffer size of 1 GB. If you read in smaller chunks, you can keep processing events and updating progress bars.


For example, I took my demo program and changed the buffer size. I used it to copy a 1.6 GB file. Here are the results:


64K buffer

$ time /tmp/a.out "in.dmg" out.bin

/tmp/a.out "in.dmg" out.bin 0.19s user 5.18s system 6% cpu 1:17.31 total


128K buffer

$ time /tmp/a.out "in.dmg" out.bin2

/tmp/a.out "in.dmg" out.bin2 0.18s user 5.19s system 18% cpu 28.864 total


256K buffer

$ time /tmp/a.out "in.dmg" out.bin3

/tmp/a.out "in.dmg" out.bin3 0.17s user 5.14s system 17% cpu 30.090 total


1 GB buffer

$ time /tmp/a.out "in.dmg" out.bin4

/tmp/a.out "in.dmg" out.bin4 0.16s user 5.64s system 18% cpu 31.113 total


In this example, 128K is actually what I should have used. Using a buffer any larger than that actually makes the program run more slowly.


Look, I realize you are a new programmer so I'm trying to help here. Having a belligerant attitude like that will be a Career Limiting Move. I make a point not to explain in Apple Support Communities the kind of software I write professionally. I prefer to engage people and explain things in a way that they can understand instead of making claims of X, Y, and Z that could never be substantiated anyway.


I would like to help you become a better programmer if you will meet me halfway. If not, then that's your loss.

Aug 4, 2012 1:41 PM in response to msuper69

The bug is in fread instruction.

--> Fread give wrong result when file len > INT_MAX.

To show that i wrote a program (copy file)

The program where i discovered than fread is buggy has a different goal.(not a copy file)

The result is the same , implementation different.


> How do you expect do get help doing that?


I don't expect to get help here since apple answer to that bug.

The answer i posted 6 month ago.

Its ok. apple is working on it,

i just yesterday said i'll post here when it will be fixed because originally i posted the bug here hoping to get

help.

The help came from apple and not from here.

to be clear.


> etresoft, stop wasting your time.


yes, thank you

Problem with fread

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.