Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: fread/fwrite |
| Date | 2012-06-18 22:05 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <jro8ns$5ub$1@reader1.panix.com> (permalink) |
| References | <2a2da336-fa08-4bfa-95b4-47f99a126877@googlegroups.com> |
In <2a2da336-fa08-4bfa-95b4-47f99a126877@googlegroups.com> "billcun@nspam.invalid" <billcun@suddenlink.net> writes:
> I want to be sure what these two functions are for.
They are for reading/writing chunks of data to/from a stream.
> To copy for example 1 2048 size bytes block at a time?
You could use them for that purpose. They can also work with other
block sizes.
> I usually use fgetc and fputc.
It can be more efficient to work with whole blocks at once, rather than
one character at a time.
> But I would please like for someone if they would to show me an example
> of these functions.
Here's a sample using fread to read up to 1000 characters from a file:
char buf[1000];
FILE *fp;
fp = fopen("filename.txt", "r");
bytes_read = fread(buf, 1, sizeof(buf) - 1, fp);
buf[bytes_read] = 0;
fclose(fp);
Here's a sample using fwrite to write a fixed message to a file:
char *message = "Hello earthlings.";
FILE *fp;
fp = fopen("message.txt", "w");
bytes_written = fwrite(message, 1, strlen(message), fp);
fclose(fp);
The first argument to fread/fwrite is a pointer to the buffer being read
or written.
The second argument is the size of each element being read/written. In
these examples we're writing characters, so the size is 1. If we were
writing other objects, we would use the size of those objects.
The third argument is the number of elements to be read/written.
The fourth argument is the stream to which the elements are to be
read/written.
The return value is the number of elements actually read/written, which
may be less than the number you requested, in which case you'll have to
keep calling fread/fwrite until all the data gets processed.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
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