473,792 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread/fwrite

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.

May 17 '07
30 14286
On Jul 5, 6:42 pm, Peter Nilsson <a...@acay.com. auwrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
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
Is size_t and all such stuffs portable in c? If not what to make it
so?
Guru Jois

Jul 8 '07 #11
Guru Jois wrote:
On Jul 5, 6:42 pm, Peter Nilsson <a...@acay.com. auwrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
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.

Jul 8 '07 #12
Guru Jois wrote:
>
.... 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.

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

--
Posted via a free Usenet account from http://www.teranews.com

Jul 8 '07 #13
On Jul 8, 4:03 am, CBFalconer <cbfalco...@yah oo.comwrote:
Guru Jois wrote:

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

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

--
Posted via a free Usenet account fromhttp://www.teranews.co m
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

Jul 9 '07 #14
Guru Jois wrote:
On Jul 8, 4:03 am, CBFalconer <cbfalco...@yah oo.comwrote:
Guru Jois wrote:

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

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.u k/cbook/cbook.html>
<http://www.cpax.org.uk/prg/portable/c/index.php>

Jul 9 '07 #15
santosh said:

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

<http://www.eskimo.com/~scs/cclass/>
Please allow me to add the Tom Torfs tutorial URL:

<http://cprog.tomsweb.n et/cintro.html>

Tom used to be a regular (and highly respected) comp.lang.c contributor.
Then, one day, he asked a question about C99, and it transpired that he
was writing a C99 preprocessor. He has not been seen since.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '07 #16
On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
>Guru Jois wrote:
>On Jul 5, 6:42 pm, Peter Nilsson <a...@acay.com. auwrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
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
Jul 9 '07 #17
¬a\/b said:
On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
<snip>
>>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
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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '07 #18
¬a\/b wrote, On 09/07/07 07:42:
On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
>Guru Jois wrote:
>>On Jul 5, 6:42 pm, Peter Nilsson <a...@acay.com. auwrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
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.
--
Flash Gordon
Jul 9 '07 #19
On Mon, 09 Jul 2007 08:19:40 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
>¬a\/b wrote, On 09/07/07 07:42:
>On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
>>Guru Jois wrote:
On Jul 5, 6:42 pm, Peter Nilsson <a...@acay.com. auwrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>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
Jul 9 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
2
15578
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ==== The structure of the file is:
4
9290
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
6322
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
17194
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
2
3080
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth strange happen, the data read from a file is not the same the data I supposed to write. Since I can't view the binary data file, I don't know what's wrong. Here is my code, please help me, Thanks a lot! Elisa #include <stdio.h> #define...
1
5576
by: rohit deshpande | last post by:
i am writing a program in c,c++....... I want to read one file and write its contents using fread and fwrite functions. the program i hv written has no errors. but output file is not same as that of read file. main() of program is like this........ void main() { FILE *readfile,*writefile; unsigned long ml,mr; char ch; int noc=0,blen=0;
5
2373
by: loudking | last post by:
Dear all, I encountered a problem with fread and fwrite. If I am going to copy a file using the same string, it will succeed char *file_content; struct stat buf; FILE *fp, *new_fp; fread(file_content, sizeof(char), buf.st_size, fp);
4
3777
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triangles sharing a common edge) indexes. Now, I'm storing this data in these struct's: struct Face { Face() {} Face(GLushort v0,...
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.