Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fread/fwrite

Reply
Thread Tools

fread/fwrite

 
 
empriser
Guest
Posts: n/a
 
      05-17-2007
How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      05-17-2007
empriser wrote:

> How to use fread/fwrite copy a file.
> When reach file's end, fread return 0, I don't konw how many bytes
> in buf.


None.

Don't you have documentation for these functions? Or man pages? Or
access to Google?

--
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      05-17-2007
On 17 May 2007 00:52:29 -0700, empriser <(E-Mail Removed)> wrote:

>How to use fread/fwrite copy a file.
>When reach file's end, fread return 0, I don't konw how many bytes
>in buf.


It depends on how code the fread. If you specify 1 block of n
characters and there is less than n left in the file, then it will
return 0. However, if you specify n blocks of 1 character each, then
it will return the number of blocks read (which will be exactly the
same as the number of characters read).


Remove del for email
 
Reply With Quote
 
empriser
Guest
Posts: n/a
 
      07-05-2007
On May 17, 8:25 pm, Barry Schwarz <(E-Mail Removed)> wrote:
> On 17 May 2007 00:52:29 -0700, empriser <(E-Mail Removed)> wrote:
>
> >How to use fread/fwrite copy a file.
> >When reach file's end, fread return 0, I don't konw how many bytes
> >in buf.

>
> It depends on how code the fread. If you specify 1 block of n
> characters and there is less than n left in the file, then it will
> return 0. However, if you specify n blocks of 1 character each, then
> it will return the number of blocks read (which will be exactly the
> same as the number of characters read).
>
> Remove del for email


Yes my buffer size is n (n > 1).
How do I know there are how many bytes in buffer, after last call
fread as if return 0.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-05-2007
empriser wrote:
> On May 17, 8:25 pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On 17 May 2007 00:52:29 -0700, empriser <(E-Mail Removed)> wrote:
>>
>>> How to use fread/fwrite copy a file.
>>> When reach file's end, fread return 0, I don't konw how many bytes
>>> in buf.

>> It depends on how code the fread. If you specify 1 block of n
>> characters and there is less than n left in the file, then it will
>> return 0. However, if you specify n blocks of 1 character each, then
>> it will return the number of blocks read (which will be exactly the
>> same as the number of characters read).
>>
>> Remove del for email

>
> Yes my buffer size is n (n > 1).
> How do I know there are how many bytes in buffer, after last call
> fread as if return 0.


fread returns the number of elements that were read and
stored in the buffer. If fread returns 10, it successfully
read and stored 10 elements. If fread returns 0, it read
and stored ... <wait for it> ... no elements.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-06-2007
On Thu, 05 Jul 2007 05:00:26 -0000, empriser <(E-Mail Removed)>
wrote:

>On May 17, 8:25 pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On 17 May 2007 00:52:29 -0700, empriser <(E-Mail Removed)> wrote:
>>
>> >How to use fread/fwrite copy a file.
>> >When reach file's end, fread return 0, I don't konw how many bytes
>> >in buf.

>>
>> It depends on how code the fread. If you specify 1 block of n
>> characters and there is less than n left in the file, then it will
>> return 0. However, if you specify n blocks of 1 character each, then
>> it will return the number of blocks read (which will be exactly the
>> same as the number of characters read).
>>
>> Remove del for email

>
>Yes my buffer size is n (n > 1).
>How do I know there are how many bytes in buffer, after last call
>fread as if return 0.


If you don't show your code, everything is just a guess. As long as
we are guessing, I guess that the number of bytes read is equal to the
square root of the address of fread (when cast to an unsigned short).


Remove del for email
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-06-2007
Eric Sosman <(E-Mail Removed)> wrote:
> empriser wrote:
> > ...
> > Yes my buffer size is n (n > 1).
> > How do I know there are how many bytes in buffer,
> > after last call fread as if return 0.

>
> fread returns the number of elements that were
> read and stored in the buffer. If fread returns 10,
> it successfully read and stored 10 elements. If
> fread returns 0, it read and stored ... <wait for
> it> ... no elements.


In other words, code as...

static char buf[1024];
size_t r = fread(buf, 1, sizeof buf, fp);

....rather than...

static char buf[1024];
size_t r = fread(buf, sizeof buf, 1, fp);

Assuming no read errors, if there are 123 characters
left in the stream, the former will return 0 into r,
whereas the latter will return 123. Both calls will
read the same number of characters though.

--
Peter

 
Reply With Quote
 
empriser
Guest
Posts: n/a
 
      07-07-2007
OK I write a easy program to show it.
fread( buf, sizeof(buf), 1, rf );
fwrite( buf, sizeof(buf), 1, wf );

If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
file.

int main( int argc, char **argv )
{
int i;
char buf[1024];
FILE *rf, *wf;

rf = fopen( argv[1], "r" );
wf = fopen( argv[2], "w" );

i = fread( buf, sizeof(buf), 1, rf );
while( i > 0 ){
fwrite( buf, sizeof(buf), 1, wf );
i = fread( buf, sizeof(buf), 1, rf );
}

fclose( rf );
fclose( wf );
return 0;
}

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-07-2007
On Sat, 07 Jul 2007 03:04:46 -0000, empriser <(E-Mail Removed)>
wrote:

>OK I write a easy program to show it.
>fread( buf, sizeof(buf), 1, rf );
>fwrite( buf, sizeof(buf), 1, wf );
>
>If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
>file.


By changing fread to fgetc and fwrite to fputc.

What is with the spate of "I've got this broken code I don't want to
change. How do I make it work?" messages. The only way to fix broken
code is to change it. It you don't change it, it remains broken.

If I insist on putting water in my gas tank, how do I get my car to
run?

>
>int main( int argc, char **argv )
>{
> int i;
> char buf[1024];
> FILE *rf, *wf;
>
> rf = fopen( argv[1], "r" );
> wf = fopen( argv[2], "w" );
>
> i = fread( buf, sizeof(buf), 1, rf );
> while( i > 0 ){
> fwrite( buf, sizeof(buf), 1, wf );
> i = fread( buf, sizeof(buf), 1, rf );
> }
>
> fclose( rf );
> fclose( wf );
> return 0;
>}



Remove del for email
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-07-2007
empriser wrote:
> OK I write a easy program to show it.
> fread( buf, sizeof(buf), 1, rf );
> fwrite( buf, sizeof(buf), 1, wf );
>
> If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
> file.
>
> int main( int argc, char **argv )
> {
> int i;
> char buf[1024];
> FILE *rf, *wf;
>
> rf = fopen( argv[1], "r" );
> wf = fopen( argv[2], "w" );
>
> i = fread( buf, sizeof(buf), 1, rf );
> while( i > 0 ){
> fwrite( buf, sizeof(buf), 1, wf );
> i = fread( buf, sizeof(buf), 1, rf );
> }
>
> fclose( rf );
> fclose( wf );
> return 0;
> }
>

Maybe this way..

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *in, *out;
char buf[100];
size_t size;

if ((in = fopen("cp.c", "rb")) == NULL)
puts("Can't open cp.c"), exit(EXIT_FAILURE);

if ((out = fopen("cp.x", "wb")) == NULL)
puts("Can't make cp.x"), exit(EXIT_FAILURE);

while ((size = fread(buf, 1, sizeof buf, in)) > 0)
fwrite(buf, 1, size, out);

fclose(in);
fclose(out);
return 0;
}

Note that cp.c is the name of this source file.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments