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.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
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.
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
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.
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.
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.
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..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;
}
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.
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?
Guru said:.... snip ...
Is size_t and all such stuffs portable in c? If not what to make
it so?
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
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/>
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.
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
only uint8_t uint16_t uint32_t, uint64_t are portable
size_t is defined in the Standard, so it's certainly portable. NewGuru 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?
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
¬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.
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.