Date: | Wed, 12 Apr 2006 23:48:56 +0300 |
From: | Diomidis Spinellis <dds@aueb.gr> |
Organization: | Athens University of Economics and Business |
User-Agent: | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 |
MIME-Version: | 1.0 |
Newsgroups: | comp.lang.c |
Subject: | Re: fgetc() vs. fread() |
References: | <20060412182901.2bb881a6.ahman@cyberspace.org> |
In-Reply-To: | <20060412182901.2bb881a6.ahman@cyberspace.org> |
Content-Type: | text/plain; charset=ISO-8859-1; format=flowed |
Content-Transfer-Encoding: | quoted-printable |
M. =C5hman wrote: > 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)? >=20 > 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? On systems (like Microsoft Windows) that have different conventions for=20 text and "binary" files opening a file in binary mode will affect both=20 fread/fwrite and fgetc/fputc. The expalanation for what you've read is = that the fread/fwrite interface is more suited for processing files=20 containing binary data, whereas the getc/putc interface is more suited=20 for processing text files. For example, the Unix dump command reads user data records from the=20 (binary-structured) utmp file directly into the corresponding utmp=20 struct using the following fread call: struct utmp utmp; if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) !=3D 1) while the Unix cat command removes successive newlines from=20 (text-structured) files using the following getc call: for (prev =3D '\n'; (ch =3D getc(fp)) !=3D EOF; prev =3D ch) { if (prev =3D=3D '\n') { if (ch =3D=3D '\n') { Nothing prevents you from processing text files with fread/fwrite (for=20 example by reading chunks into a large buffer) or binary files with=20 getc/putc (for example by assembling the data into variables by=20 hand-crafted code). --=20 Diomidis Spinellis Code Quality: The Open Source Perspective (Addison-Wesley 2006) http://www.spinellis.gr/codequality