Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Using fread function Date: Mon, 30 Jan 2017 11:58:45 -0800 Organization: None to speak of Lines: 40 Message-ID: References: <02c75a0c-84d7-4669-8bde-472730c7f8d3@googlegroups.com> <18a13059-6cfd-47e9-9844-bd18f2ae6731@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="a9fae22b5f567322e337da4e19df7ab9"; logging-data="25319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DBp+PbvoyLmaZQumLI/3n" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:5CvjnhWrQz+/Sbad5MZLH+zbe2g= sha1:kRmnRk7U3VMNVIRLIF0LGwFXrI4= Xref: csiph.com comp.lang.c:102789 Malcolm McLean writes: > On Sunday, January 29, 2017 at 6:07:29 PM UTC, janus wrote: >> I just noticed that swapping the positions of the inner parameters >> does not have any material effect on result. That led me to think >> that "fread" function just multiple the two parameters. >> >> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); >> >> size_t fread(void *ptr, size_t nmemb, size_t size, FILE *stream); >> >> Is my position correct? >> > Essentially yes. Essentially no. > Having two parameters is pretty useless and you might as well pass > in a size of memory buffer you wish to fill. It also tends to encourage code such > as > > fread(x, sizeof(double), 10, fp); > > which isn't very portable. How is that not portable (assuming x points to a chunk of memory big enough to hold 10 doubles)? > The only slight advantage is that it tells you how many doubles were read correctly. > The number of times the information "7 out of 10" is more useful than "not all ten" > is vanishingly low, however. If the distinction isn't useful, you can read a single chunk of 10 doubles. If it is, you can read 10 elements of sizeof(double) bytes each. I presume that flexibility is quite intentional. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"