mitchemt@silver.ucs.indiana.edu (02/17/90)
I am trying to get fread to work with a structure I have defined. It seems
that turbo c won't read correctly. The structure is defines as follows:
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
someone please send me and example of one that would work with the code I
have written.
Terrence Mitchem
ps: any help is GREATLY appreciated.
cpcahil@virtech.uucp (Conor P. Cahill) (02/18/90)
In article <9600005@silver> mitchemt@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 | +-----------------------------------------------------------------------+
raw@math.arizona.edu (Rich Walters) (02/19/90)
In article <1990Feb17.194423.12047@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes: >In article <9600005@silver> mitchemt@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 -------------------------------------------------------------------------------
cpcahil@virtech.uucp (Conor P. Cahill) (02/20/90)
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 | +-----------------------------------------------------------------------+