Author |
Message |
A P R Coop #1 / 23
|
 Is this a good Idea??
Quote:
>Subject: Is this a good Idea??
>Date: 6 Oct 1995 09:58:24 -0400 >We are trying to create a dataset which will eventually be transferred >to many different users on many different machines (unix, mac, dos, >etc). We plan to create the dataset and a number of routines for >extracting data from the dataset into various software import formats. >The question is this, I am going to be programming in ANSI C. I want >to read and write the files in binary format. Every variable or >number that I write to the file is between 0 and 255 (one byte). I >plan to write all the variables (characters and integers) as >characters with a simple fprintf statement. We will then make the >datasets and the ANSI C routines available to others who will >retrieved them over the internet. >What I want to know is if this is a bad idea for any reason. If the >person grabs the C code they need and compile it on their system, is >there any reason to believe they couldn't read the data file? >We are at a starting point now and want to preclude any problems now. >Thanks for any input. >Jay Wright >Rutgers University
Wouldn't it be better to use fputc/fgetc? These explicitly put and get a BYTE, not a character. Using fprintf/fscanf would give you problems with CR/LF characters. A. Paul R. Cooper British Antarctic Survey
***** Any opinions given here are my own, ***** ***** and not necessarily those of British Antarctic Survey *****
|
Tue, 24 Mar 1998 03:00:00 GMT |
|
 |
Kurt Watz #2 / 23
|
 Is this a good Idea??
Quote: >Wouldn't it be better to use fputc/fgetc? These explicitly put and get a BYTE, >not a character. Using fprintf/fscanf would give you problems with CR/LF >characters.
As would fputc/fgetc. BTW what is the difference between a byte and a char, as far as C is concerned? (Hint: Both words denote the same as long as you are talking about C). CR/LF translation does not depend on which i/o function you use, but on whether the file is opened in "text mode" or in "binary mode" on systems where this makes any difference. Kurt -- | Kurt Watzka Phone : +49-89-2180-6254
|
Tue, 24 Mar 1998 03:00:00 GMT |
|
 |
Paul Lo #3 / 23
|
 Is this a good Idea??
Chapel) writes: Quote:
>>As would fputc/fgetc. BTW what is the difference between a byte and >>a char, as far as C is concerned? (Hint: Both words denote the same >>as long as you are talking about C). >I don't think that's true. Isn't a char definde to be *at least* 8 bits >(in other words a byte) in the ANSI C standard? A char could be 2 bytes, >or 4 bytes, or 256 bytes, for that matter.
This is a common misunderstanding. A byte, in Standard C, is not an 8-bit unit of storage: "A byte is composed of a contiguous sequence of bits, the number of which is implementation-defined." And that number is defined by the value of CHAR_BIT in limits.h, which must be at least 8. So, for example, you could have 8-bit bytes, 9-bit bytes, or 16-bit bytes. Because "the sizeof operator yields the size (in bytes) of its operand" and the result is1 for the char types, a char object always occupies exactly one byte of storage, not "2 bytes, or 4 bytes, or 256 bytes," at least using Standard C's definition of a "byte." When talking about bytes in the context of C, you have to be very careful. Do you mean simply 8-bits or the formal, more elusive definition provided in the Standard?
|
Thu, 26 Mar 1998 03:00:00 GMT |
|
 |
Hans Steffa #4 / 23
|
 Is this a good Idea??
Quote:
>BTW what is the difference between a byte and >a char, as far as C is concerned? (Hint: Both words denote the same >as long as you are talking about C).
I am not sure whether C defines the Byte to be the size of an character variable. Hans Friedrich Steffani -- Hans Friedrich Steffani Institut fuer Elektrische Maschinen und Antriebe TU Chemnitz-Zwickau
|
Fri, 27 Mar 1998 03:00:00 GMT |
|
 |
A P R Coop #5 / 23
|
 Is this a good Idea??
Quote:
>Subject: Re: Is this a good Idea??
>Date: 6 Oct 1995 19:34:21 GMT
>>Wouldn't it be better to use fputc/fgetc? These explicitly put and get a BYTE, >>not a character. Using fprintf/fscanf would give you problems with CR/LF >>characters. >As would fputc/fgetc. BTW what is the difference between a byte and >a char, as far as C is concerned? (Hint: Both words denote the same >as long as you are talking about C). CR/LF translation does not >depend on which i/o function you use, but on whether the file is >opened in "text mode" or in "binary mode" on systems where this >makes any difference. >Kurt >-- >| Kurt Watzka Phone : +49-89-2180-6254
I beg your pardon - there is a tiny caveat in scanf that I missed. I thought that scanf ALWAYS recognized special characters (space, tab or newline) as field delimiters, and would not read them. It turns out that when reading single characters, it will read the file byte by byte. HOWEVER! I would still do bytewise I/O using fgetc/fputc, because some characters are special, not just to C, but to the operating system. In particular, end-of-line is amazingly system specific. UNIX filesystems follow one convention, DOS another, various IBM operating systems follow several conventions all at once depending on file header settings! I would say that my distinction between characters and bytes is valid, because a character need not map one-to-one onto a byte. A character may be none, one or many bytes. A byte is a byte (unless you remember some old systems with 7 bit bytes....) At a slightly higher level, I am using DEC Alpha workstations at the moment. I keep coming accross major problems with commercial software where people have obviously made assumptions about the wordlength of the host machine. It is amazing how many people have written software which ASSUMES that an int is two bytes! A. Paul R. Cooper British Antarctic Survey
***** Any opinions given here are my own, ***** ***** and not necessarily those of British Antarctic Survey *****
|
Fri, 27 Mar 1998 03:00:00 GMT |
|
 |
Steve Summ #6 / 23
|
 Is this a good Idea??
[Unknown (to me) newsgroups comp.lang and comp.std trimmed.]
Quote: (Steve E. Chapel) writes:
>> ...BTW what is the difference between a byte and >> a char, as far as C is concerned? (Hint: Both words denote the same >> as long as you are talking about C). > I don't think that's true. Isn't a char definde to be *at least* 8 bits > (in other words a byte) in the ANSI C standard? A char could be 2 bytes, > or 4 bytes, or 256 bytes, for that matter.
I'm not sure what point Kurt was trying to make, but here's what the forthcoming "even longer" version of the comp.lang.c FAQ list (to appear in early November as C Programming FAQs: Frequently Asked Questions, Addison-Wesley, ISBN 0-201-84519-9) has to say about the C definition of a "byte": 8.10: I'm starting to think about multinational character sets, and I'm worried about the implications of making sizeof(char) be 2 so that 16-bit character sets can be represented. A: If type char were made 16 bits, sizeof(char) would still be 1, and CHAR_BIT in <limits.h> would be 16, and it would simply be impossible to declare (or allocate with malloc()) a single 8-bit object. Traditionally, a byte is not necessarily 8 bits, but merely a smallish region of memory, usually suitable for storing one character. The C Standard follows this usage, so the bytes used by malloc() and sizeof can be more than 8 bits. [1] (The Standard does not allow them to be less.) To allow manipulation of multinational character sets without requiring an expansion of type char, ANSI/ISO C defines the "wide" character type wchar_t, and corresponding wide string literals, and functions for manipulating and converting strings of wide characters. References: ANSI Sec. 2.2.1.2, Sec. 3.1.3.4, Sec. 3.1.4, Sec. 4.1.5, Sec. 4.10.7, Sec. 4.10.8 ISO Sec. 5.2.1.2, Sec. 6.1.3.4, Sec. 6.1.4, Sec. 7.1.6, Sec. 7.10.7, Sec. 7.10.8 Rationale Sec. 2.2.1.2 H&S Sec. 2.7.3 pp. 29-30, Sec. 2.7.4 p. 33, Sec. 11.1 p. 293, Secs. 11.7,11.8 pp. 303-310 __________ 1. Formally, bytes of exactly 8 bits are referred to as "octets." Steve Summit
|
Sat, 28 Mar 1998 03:00:00 GMT |
|
 |
Gordon Burdi #7 / 23
|
 Is this a good Idea??
Quote: >>BTW what is the difference between a byte and >>a char, as far as C is concerned? (Hint: Both words denote the same >>as long as you are talking about C). >I am not sure whether C defines the Byte to be >the size of an character variable.
By the C definition, sizeof(char) == 1, and the units of sizeof are in bytes. Therefore a byte and a char have the same size. A char is a recognized C type; a byte is not. The C definition also says that a char has >= 8 bits, NOT == 8 bits. The same would also apply to a byte. Gordon L. Burditt sneaky.lonestar.org!gordon
|
Mon, 30 Mar 1998 03:00:00 GMT |
|
 |
Peter Wu's Accou #8 / 23
|
 Is this a good Idea??
: >>BTW what is the difference between a byte and : >>a char, as far as C is concerned? (Hint: Both words denote the same : >>as long as you are talking about C). : > : >I am not sure whether C defines the Byte to be : >the size of an character variable. : By the C definition, sizeof(char) == 1, and the units of sizeof are : in bytes. Therefore a byte and a char have the same size. : A char is a recognized C type; a byte is not. I am not an expert in the field of compiler nor programming. But I want to ask whether "byte" can have "signed" and "unsigned" properties, like those of "char", that contribute to many portablilty problems and bugs. IMO, "byte" is always signed. Right? or is it machine/implementation dependent? P.S. I have never use the type "byte" in my C programs, pls. forgive me if my question is too silly :> -- Peter Wu E8-P)
|
Sat, 04 Apr 1998 03:00:00 GMT |
|
 |
bhoo.. #9 / 23
|
 Is this a good Idea??
Quote: > BYTE AND CHAR, WAS RE: IS THIS A GOOD IDEA?? > Newsgroups: comp.lang.c,comp.std.c,comp.programming,comp.lang,comp.std > Post a followup article to newsgroup(s)
> Send e-mail reply to: Hans Steffani > Date: 9 Oct 95 08:18:50 GMT > Organization: University of Technology Chemnitz, FRG
>>BTW what is the difference between a byte and >>a char, as far as C is concerned? (Hint: Both words denote the same >>as long as you are talking about C). >I am not sure whether C defines the Byte to be >the size of an character variable. >Hans Friedrich Steffani >-- >Hans Friedrich Steffani >Institut fuer Elektrische Maschinen und Antriebe >TU Chemnitz-Zwickau
Yeah, in C, the char data type is 1 byte. -- miracles will have their claimers more will bow to Rome he and she are in the house but there's only me at home
|
Sun, 05 Apr 1998 03:00:00 GMT |
|
 |
Lawrence Cro #10 / 23
|
 Is this a good Idea??
Quote:
>Yeah, in C, the char data type is 1 byte.
Quote:
>And of course, an int and a long may -also- each be 1 byte.
If so, the byte must be at least 32 bits wide. -- Lawrence Crowl 503-737-2554 Computer Science Department
http://www.cs.orst.edu/~crowl/ Corvallis, Oregon, 97331-3202
|
Thu, 09 Apr 1998 03:00:00 GMT |
|
 |
Simon Tath #11 / 23
|
 Is this a good Idea??
Quote:
>: >And of course, an int and a long may -also- each be 1 byte. >: If so, the byte must be at least 32 bits wide. >I think you'll find that he was making reference to the fact that the C >standard does not specify the "size" of data types (this being largely a >machine dependent issue) - as such it is up to the compiler writer to >specify this.
Not quite - Kernighan & Ritchie, second edition, specifies that "int" and "short" should be at least 16 bits, and "long" should be at least 32. It *is* up to the compiler writer, of course, but those are minimum limits. (Interesting to note that most compilers exceed at least one of those limits, except real-mode DOS ones, which stick firmly to *all* the minima... :) --
\ I /\/\ O /\/ http://callisto.girton.cam.ac.uk/users/sgt20 / ~~~~~~~~~~~~~~~~~ Trinity College, Cambridge, CB2 1TQ, England
|
Thu, 09 Apr 1998 03:00:00 GMT |
|
|
Page 1 of 2
|
[ 23 post ] |
|
Go to page:
[1]
[2] |
|