Path: utzoo!attcan!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: fread help Message-ID: <1990Feb19.161706.594@virtech.uucp> Date: 19 Feb 90 16:17:06 GMT References: <9600005@silver> <1990Feb17.194423.12047@virtech.uucp> <1432@amethyst.math.arizona.edu> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 39 In article <1432@amethyst.math.arizona.edu> raw@math.arizona.edu (Rich Walters) writes: >In article <1990Feb17.194423.12047@virtech.uucp> cpcahil@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 | +-----------------------------------------------------------------------+