fread/fwrite

E

empriser

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.
 
C

Chris Dollin

empriser said:
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?
 
B

Barry Schwarz

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
 
E

empriser

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.
 
E

Eric Sosman

empriser said:
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.
 
B

Barry Schwarz

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
 
P

Peter Nilsson

Eric Sosman said:
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.
 
E

empriser

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;
}
 
B

Barry Schwarz

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
 
J

Joe Wright

empriser said:
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.
 
G

Guru Jois

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.

Is size_t and all such stuffs portable in c? If not what to make it
so?


Guru Jois
 
S

santosh

Guru said:
Eric Sosman said:
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.

Is size_t and all such stuffs portable in c? If not what to make it
so?

size_t is defined in the Standard, so it's certainly portable. New
users to C tend to have some confusion over using size_t objects with
printf and family. Generally using the lu format specifier with a cast
to the appropriate type is recommended. If you can, and want to, use
C99's additions the zu specifier is specifically there for size_t.
 
C

CBFalconer

Guru said:
.... snip ...

Is size_t and all such stuffs portable in c? If not what to make
it so?

Yes. That is, in part, why this newsgroup limits the topic to
STANDARD C, and considers references to system dependent things as
being off-topic. Read the standard.
 
G

Guru Jois

Guru Jois wrote:

... snip ...


Yes. That is, in part, why this newsgroup limits the topic to
STANDARD C, and considers references to system dependent things as
being off-topic. Read the standard.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

How and where can I read to understand the perfect knowledge about C
standards.
Any docs/links to learn pure portable programs for C??? Docs
prefered..

Guru Jois
 
S

santosh

Guru said:
How and where can I read to understand the perfect knowledge about C
standards.
Any docs/links to learn pure portable programs for C??? Docs
prefered..

As far as online tutorials are concerned I'll recommend Steve Summit's
one:

<http://www.eskimo.com/~scs/cclass/>

Also he maintains the very useful C FAQ:

<http://www.c-faq.com/>

There's also a clc "wiki":

<http://clc-wiki.net/>

Other resources include:

<http://www.lysator.liu.se/c/>
<http://www.dinkumware.com/manuals/>
<http://www-ccs.ucsd.edu/c/>
<http://www.open-std.org/jtc1/sc22/wg14/> - Site for the draft
Standard.
<http://www.knosof.co.uk/cbook/cbook.html>
<http://www.cpax.org.uk/prg/portable/c/index.php>
 
¬

¬a\\/b

Guru said:
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.

Is size_t and all such stuffs portable in c? If not what to make it
so?

size_t is defined in the Standard, so it's certainly portable. New
users to C tend to have some confusion over using size_t objects with
printf and family. Generally using the lu format specifier with a cast
to the appropriate type is recommended. If you can, and want to, use
C99's additions the zu specifier is specifically there for size_t.

size_t is not portable because you all don't know the size it has
only uint8_t uint16_t uint32_t, uint64_t are portable
 
R

Richard Heathfield

¬a\/b said:
On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:


size_t is not portable because you all don't know the size it has

Nonsense. It is exactly sizeof(size_t) bytes in size.
only uint8_t uint16_t uint32_t, uint64_t are portable

My implementation has never heard of them, and gives me compilation
errors when I try to use them. So much for "portable".

But my implementation understands size_t just fine, thanks.
 
F

Flash Gordon

¬a\/b wrote, On 09/07/07 07:42:
Guru said:
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.
Is size_t and all such stuffs portable in c? If not what to make it
so?
size_t is defined in the Standard, so it's certainly portable. New
users to C tend to have some confusion over using size_t objects with
printf and family. Generally using the lu format specifier with a cast
to the appropriate type is recommended. If you can, and want to, use
C99's additions the zu specifier is specifically there for size_t.

size_t is not portable because you all don't know the size it has
only uint8_t uint16_t uint32_t, uint64_t are portable

Rubbish. There is no guarantee that the types you are suggesting are
available (even assuming C99) so they are less portable than size_t
which is *guaranteed* to exist. size_t is also the type returned by
fread (the function being called above), so whatever it is a variable of
type size_t is *guaranteed* to be large enough.
 
¬

¬a\\/b

¬a\/b wrote, On 09/07/07 07:42:
Guru Jois 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.
Is size_t and all such stuffs portable in c? If not what to make it
so?
size_t is defined in the Standard, so it's certainly portable. New
users to C tend to have some confusion over using size_t objects with
printf and family. Generally using the lu format specifier with a cast
to the appropriate type is recommended. If you can, and want to, use
C99's additions the zu specifier is specifically there for size_t.

size_t is not portable because you all don't know the size it has
only uint8_t uint16_t uint32_t, uint64_t are portable

Rubbish. There is no guarantee that the types you are suggesting are
available (even assuming C99) so they are less portable than size_t
which is *guaranteed* to exist. size_t is also the type returned by
fread (the function being called above), so whatever it is a variable of
type size_t is *guaranteed* to be large enough.

each data definition that not fixed the size is unportable
each operation to data operand not well definited is unportable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
fwrite() fails when called after fread() 2
PHP failed to create file 13
struct and pointer question 33
URGENT 1
strdup + fread = fail? 5
Can not read VCD file in Linux 8
Problem in reding file 3

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top