Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: fread/fwrite |
| Date | 2012-06-21 12:28 +1200 |
| Message-ID | <a4f84sF7l4U8@mid.individual.net> (permalink) |
| References | (7 earlier) <00fdd2d6-aa03-4af6-be62-d3e7091c6463@a16g2000vby.googlegroups.com> <a4f5baF7l4U6@mid.individual.net> <41c3a10f-4e64-4d58-a4d8-7e653a739d7e@l17g2000vbj.googlegroups.com> <a4f69dF7l4U7@mid.individual.net> <3f284be7-d1a3-44ba-851e-d6ff297f5843@f7g2000yqh.googlegroups.com> |
On 06/21/12 12:11 PM, Bill Cunningham wrote:
> On Jun 20, 7:56 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 06/21/12 11:47 AM, Bill Cunningham wrote:
>>
>>> On Jun 20, 7:40 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>>
>>>> As others have pointed out, you have been asking the same or similar
>>>> questions here for many years.
>>
>>>> Your original question on this thread could easily have been answered
>>>> with a quick search. Quite often the search results for your questions
>>>> include the thread from the previous time(s) you asked it!
>>
>>> I have read the man pages and googled them many times on fread and
>>> fwrite. I have tried to go by that and failed.
>>
>> The first hit I get for fread is
>>
>> http://www.cplusplus.com/reference/clibrary/cstdio/fread/
>>
>> which has a clear explanation and example.
>>
>> The first hit for "c fread example" also has a simple example.
>>
>> I'm sure there are plenty more.
>>
> <cut example code>
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main () {
> FILE * pFile;
> long lSize;
> char * buffer;
> size_t result;
>
> pFile = fopen ( "myfile.bin" , "rb" );
> if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
>
>
> Below is not really clear to me. I haven't got to fseek yet
>
> // obtain file size:
> fseek (pFile , 0 , SEEK_END);
> lSize = ftell (pFile);
> rewind (pFile);
>
> // allocate memory to contain the whole file:
> buffer = (char*) malloc (sizeof(char)*lSize);
>
> Why is there a cast above when returns a generic pointer?
So it will compile as C++ (note the name of the site!).
> if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
>
> // copy the file into the buffer:
> result = fread (buffer,1,lSize,pFile);
> if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
>
> /* the whole file is now loaded in the memory buffer. */
>
> // terminate
> fclose (pFile);
> free (buffer);
> return 0;
> }
>
> I only googled "man fread" and expected an example there.
> I wanted and example with fwrite too.
Try improving your google searches.
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
fread/fwrite "billcun@nspam.invalid" <billcun@suddenlink.net> - 2012-06-18 13:45 -0700
Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-19 09:28 +1200
Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-18 17:47 -0400
Re: fread/fwrite billcun@suddenlink.net - 2012-06-18 15:48 -0700
Re: fread/fwrite Ike Naar <ike@iceland.freeshell.org> - 2012-06-18 23:37 +0000
Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-18 21:56 -0400
Re: fread/fwrite billcun@suddenlink.net - 2012-06-19 04:54 -0700
Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-19 09:43 -0400
Re: fread/fwrite billcun@suddenlink.net - 2012-06-19 09:51 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-20 00:05 -0700
Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-20 06:45 -0400
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-20 04:20 -0700
Re: fread/fwrite tom st denis <tom@iahu.ca> - 2012-06-20 07:54 -0700
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:13 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:17 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-19 08:17 -0700
Re: fread/fwrite Mark Bluemel <mark.bluemel@googlemail.com> - 2012-06-20 01:05 -0700
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:17 -0700
Re: fread/fwrite Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-20 17:33 +0200
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:50 -0700
Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 07:45 -0400
Re: fread/fwrite Kenneth Brody <kenbrody@spamcop.net> - 2012-06-20 18:38 -0400
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 15:59 -0700
Re: fread/fwrite "BartC" <bc@freeuk.com> - 2012-06-21 00:24 +0100
Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 11:40 +1200
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 16:47 -0700
Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 11:56 +1200
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:11 -0700
Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 12:28 +1200
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:51 -0700
Re: fread/fwrite Kenneth Brody <kenbrody@spamcop.net> - 2012-06-21 15:05 -0400
Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-22 07:17 +1200
Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 16:01 -0400
Re: fread/fwrite Keith Thompson <kst-u@mib.org> - 2012-06-21 15:08 -0700
Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 18:36 -0400
Re: fread/fwrite Keith Thompson <kst-u@mib.org> - 2012-06-21 16:49 -0700
Re: fread/fwrite Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 01:30 +0100
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:51 -0700
Re: fread/fwrite Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 02:22 +0100
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-22 00:13 -0700
Re: fread/fwrite tom st denis <tom@iahu.ca> - 2012-06-21 04:22 -0700
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-22 12:14 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:33 -0700
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 16:27 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:21 -0700
Re: fread/fwrite Mark Bluemel <mark.bluemel@googlemail.com> - 2012-06-21 07:17 -0700
Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-22 00:22 -0700
Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-22 12:17 -0700
Re: fread/fwrite John Gordon <gordon@panix.com> - 2012-06-18 22:05 +0000
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 09:03 -0700
Re: fread/fwrite John Gordon <gordon@panix.com> - 2012-06-20 16:28 +0000
Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 09:53 -0700
csiph-web