C programming fread and fwrite using structures
Acording to the man page and what information that I can gather fread and fwrite work with arrays. Im using them to write data to a file in structures and then using fread to retrive the data using fseek to locate it. Im having a bit of an issue getting the data back. So is it not possible to use this to write structure data or am i not understanding how these fuctions work ?
here is the writeing part Code:
int addcd(DATANODE *infoadd,const char *database) Quote:
My listing file checks for the disk number in DATANODE and keeps going though each block untill it gets to EOF but it seams to check the first one then stop like its gotten to eof. From what i can gather EOF is not being moved when a file write is being preformed so im getting something like this. Quote:
here is an older one i worked with but ive since rewritten it so it may not be usefull Code:
|
1. rewind() moves to the beginning of file, that's why you don't have more blocks
2. I don't understand the problem with EOF, could you show a simple example with data? |
It is certainly possible to write/read structures (or anything else) to/from a file with fwrite/fread.
Perhaps the following line Quote:
Code:
fseek(basefile,discnum*sizeof(DATANODE),SEEK_SET); Since you rewind() after reading, there isn't any problem there except that it'ss unnecessary, since SEEK_SET is relative to the beginning of the file, not the current position (that's SEEK_CUR). The rewind() in the writing code is also unnecessary, since you just close the file immediately after. EDIT: (The memory leak that I thought was there is not. The weird indentation just made it look like there was one.) |
Everything that you pointed out has been fixed and refixed to try and reslove I really do wish i had an update version to show you as this is old and i found those issues before.
basicly whats going on is this there are 2 recods in the file lets say record one is test1 and record 2 is test2 i have a loop that gets data from the file in the form of temphold is a pointer to a structure that i used malloc to allocate. fread(temphold,sizeof(DATANODE),1,basefile); by the way the seek was like this fseek(basefile,(sizeof(DATANODE) * looper),SEEK_SET); looper starts at 0 and increments up from there. it then checks the value of temphold->discnum to see if it matches what was requested if not then it loops around and should get the next block if not and it gets to EOF then it should stop looping and print that it couldent locate the file then exit and return to the menu. What happens is that i reads the first block and then seams to find a EOF cause it never tries to read the second block. I dont have interent access at home ATM so i cant show you the current example of what im doing its very unforunate as this hinders your help. I am thinking at this point tho to abandon what i was trying to do as it creates more problems then it solves i guess. what my idea was to have a list of structures located on a file on the computer and have a special flag set on each block of data that determines if the block is free or not and if it is then to overwrite it. basicly it would write and read from the file when trying to locate or add a entry. What im leaning tword is to keep the list of information in memory as a linked list and then do a write operation for all the data at once when the user wants to save the file. and keeping the file a text file so i can check for problems if i need to. Keeping the data in binary format i cant check to see if the information is being written to as i intend. anyway if anyone is intrested i can get the entire program as is and send it to you via email as a tar file. ( the old one of course as i havent started writeing the new version ) |
I figured it out, It was a problem in the way I was checking for the end of a file. I was using this statement in a while loop when reading the file.
while (feof(datfile) != 0) this is not correct cause if its not = 0 then it means that the statement is true and the while loop will end. I merly changed it to while (feof(datfile) == 0) so that it will continue untill it gets to the end. I do however need to rewrite the code so that im not fseeking past the end of the file. Thanks everyone for your help |
All times are GMT -5. The time now is 09:29 PM. |