473,792 Members | 2,807 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
Ĵa\/b said:

<snip>
each data definition that not fixed the size is unportable
Why?
each operation to data operand not well definited is unportable
size_t is well-defined.

--
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 #21
Ĵa/b wrote:
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:
<snip>
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
How come then dynamically typed programming languages like Python and
Common Lisp are so portable and popular?
each operation to data operand not well definited is unportable
That's certainly true, but that's irrelevant to our discussion, since
size_t is well defined by the C Standard. What problems do you
specifically face when you use size_t?

Jul 9 '07 #22
On Mon, 09 Jul 2007 08:31:03 +0000, Richard Heathfield wrote:
>Ĵa\/b said:
>each data definition that not fixed the size is unportable

Why?
easy
>each operation to data operand not well definited is unportable

size_t is well-defined.
portable in my country means: i write a X language program;
that program run the same way in all machines
in the world that have a X language conform compiler

then you can argue how this program is portable
in the standard C language

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

int main(void)
{size_t z=0x2FFFF, h=0xFFFFFFFF;
z=z*z;
printf("Bits(si ze_t) 32 ?\n");

if(h<=0xFFFF||z <=h)
printf("No\n" );
else printf("Yes\n") ;

return 0;
}

because in each machine it could answer in a different way.
So what do you mean for the word "portable"? it is a meaningless word

in each machine where there are definitions like "size_t=num ber"
or where there are operations with size_t type variable =they are
not portable by definition (in each machine could be a different
result)

the problem is: size_t has different size each machine
but let it be size_t has 64 bits for all machine in the world
(or it is an memory only dipendent data type like a bignumber)

the problem could be for "*" function: can "*" function is the same in
all the machine and have the same result for the same operands?

the same for all other operators
the same for all UB and machine dipendent results
the same for all other "fixed all different size" data types
Jul 9 '07 #23
Ĵa\/b said:

<snip>
portable in my country means: i write a X language program;
that program run the same way in all machines
in the world that have a X language conform compiler

then you can argue how this program is portable
in the standard C language

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

int main(void)
{size_t z=0x2FFFF, h=0xFFFFFFFF;
No, this isn't portable, because C doesn't guarantee size_t can store
values as high as 0x2FFFF.

The fact that you can write a non-portable program using size_t does not
mean size_t is not portable.
So what do you mean for the word "portable"?
I mean that the program will achieve the same computational objective,
regardless of the implementation used to translate it.
it is a meaningless word
To you, maybe, but not to me.
in each machine where there are definitions like "size_t=num ber"
or where there are operations with size_t type variable =they are
not portable by definition (in each machine could be a different
result)
Two levers can be of different lengths, yet both can provide leverage,
and you can use either to shift a rock. Just as the concept of
"leverage" is portable across levers, so the concept of object size is
portable across implementations - and size_t is the type with which we
measure object sizes.

--
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 #24
In article <31************ *************** *****@4ax.com>, Ĵa\\/b <al@f.gwrote:
>portable in my country means: i write a X language program;
that program run the same way in all machines
in the world that have a X language conform compiler
>then you can argue how this program is portable
in the standard C language
[...]
>because in each machine it could answer in a different way.
"running the same way" does not mean the same as "will
produce the same answer". For example, a program to print
out the current calendar time might have the same code
on all the systems, but it is going to produce a different
answer when run on each one of them.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jul 9 '07 #25
Ĵa/b wrote:
On Mon, 09 Jul 2007 08:31:03 +0000, Richard Heathfield wrote:

size_t is well-defined.

portable in my country means: i write a X language program;
that program run the same way in all machines
in the world that have a X language conform compiler

then you can argue how this program is portable
in the standard C language

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

int main(void)
{size_t z=0x2FFFF, h=0xFFFFFFFF;
You can't expect to portably store arbitrarily large values. size_t
can store values in the range of 0 ... SIZE_MAX. What's ill-defined
about this?
z=z*z;
Here you're performing an arithmetic calculation without checking for
the possibility of wrap-around. Again results might vary across
implementations , but this is not exclusive to size_t types.
printf("Bits(si ze_t) 32 ?\n");

if(h<=0xFFFF||z <=h)
You're comparing objects which might not hold the values intended for
them.
printf("No\n" );
else printf("Yes\n") ;

return 0;
}

because in each machine it could answer in a different way.
So what do you mean for the word "portable"? it is a meaningless word
And in what way is this exclusive to size_t. This is true for all of
C's numeric types. Each type's size and range are implementation
dependant except that certain minimum values are guaranteed by the
Standard. That's the price you'll have to pay for using an efficient,
statically typed language.

<snip>
the problem is: size_t has different size each machine
but let it be size_t has 64 bits for all machine in the world
(or it is an memory only dipendent data type like a bignumber)
If you want a language with dynamically sized types, there're plenty
of them floating around. C was not defined to be such a language. On
the other hand, if you stick to what's guaranteed by the Standard, you
_can_ achieve a very high level of source code portability. The
problem is, you want guarantees the Standard is not providing.

<snip>

Jul 9 '07 #26
On Mon, 09 Jul 2007 10:19:09 +0200, "Ĵa\\/b" <al@f.gwrote:
snip
>each data definition that not fixed the size is unportable
each operation to data operand not well definited is unportable
Well then, you must not use int either since there are a plethora of
16-bit and 32-bit implementations . You probably don't use char either
since we know sizeof(char) is 1 but we don't know anything about
CHAR_BIT other than it is at least 8.
Remove del for email
Jul 10 '07 #27
On Jul 8, 11:25 pm, santosh <santosh....@gm ail.comwrote:
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>
Thanks Santhosh

Jul 10 '07 #28
On Jul 8, 11:41 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
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
Thanks to Richard too.

Jul 10 '07 #29
Richard Heathfield wrote:
Ĵa\/b said:

<snip>
>each data definition that not fixed the size is unportable

Why?
>each operation to data operand not well definited is unportable

size_t is well-defined.
Hint: Ĵa\/b is a known troll.

--
<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 11 '07 #30

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
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.