fread help

Conor P. Cahill cpcahil at virtech.uucp
Tue Feb 20 03:17:06 AEST 1990


In article <1432 at amethyst.math.arizona.edu> raw at math.arizona.edu (Rich Walters) writes:
>In article <1990Feb17.194423.12047 at virtech.uucp> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
>>What you probably want to do is:
>>	struct e {
>>		  char indicator;
>>		  char description[size_for_desc];
>>		  char path[size_for_path];
>>		 };
>>
>>Then your fread(),  fwrites(), etc will work as you want them to 
>
>
>I don't think that this will work very well either.  If the file is less than
>1 + size_for_desc + size_for_path then the strings may/will be scrambled.

This is intended to be use for reading and WRITING the file.  If you don't use
the same mechanism for reading that you use for writing (and vice-versa) you
will almost always have trouble with the read end.

>  This
>may be avoided if the strings are padded/truncated to the desired length.  The
>problem is that fread() will end each string with \n instead of \0 so that it

This will not be the case if the user used fwrite to write the data.  In fact
if you use fwrite with the above structure  you wont have any \n's in the file
at all (unless they happened to appear in the strings themselves).

>is difficult to use them immediately with _any_ C routine that requires a
>string.  Of course the \n can be replace with the \0 needed, but is seems to me
>to be easier, IMHO, to use fgets/fgetc in the first place.

fgets and/or fgetc are usefull for ascii (i.e. plain text) files as opposed
to binary files.  While fread() and/or fwrite() can be used to access ascii
files, they are more appropraitely used for binary and/or fixed field files.
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list