fread help

Conor P. Cahill cpcahil at virtech.uucp
Sun Feb 18 06:44:23 AEST 1990


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. Is my code wrong or is the file wrong. If it is the file could 

The problem is that when you write thay structure to the file only the
values of the pointers for description and path get written to the file, not
the data that the pointers point to.

You should not use pointers when reading and writing to a file unless you
are guarranteed that only the current run of your program is using said
file and that the values that the pointers point to have the appropriate
existance scope.

What you probably want to do is:
	struct e {
		  char indicator;
		  char description[size_for_desc];
		  char path[size_for_path];
		 };
(where size_for_desc and size_for_path are the appropriate constant values)

Then your fread(),  fwrites(), etc will work as you want them to 



-- 
+-----------------------------------------------------------------------+
| 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