fread help

Rich Walters raw at math.arizona.edu
Mon Feb 19 15:39:25 AEST 1990


In article <1990Feb17.194423.12047 at virtech.uucp> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
>In article <9600005 at silver> mitchemt at silver.ucs.indiana.edu writes:
>>struct e {
>>	  char indicator;
>>	  char *description;
>>	  char *path;
>>	 };
>>typedef struct e ENTRY;
>>
>>When I try to fread, I get the correct value for 'indicator', but junk for the
>>other two.
>
		<deleted stuff>
>
>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
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
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.




			Richard Walter


-------------------------------------------------------------------------------

			Keep on crunching those numbers

-------------------------------------------------------------------------------



More information about the Comp.lang.c mailing list