I've got some code that was working on a server until it was moved to a different system. The problem seems to be here:
given a structure I've defined elsewhere:
user1_type user1; /* structure containing user data */
user1_type *user1_ptr=&user1;
this routine appends the record to the end of the file
if ((dfile=fopen(filename,"ab+"))==NULL)
error_message("Unable to open file for append.",filename,1);
else { /* append data */
user1.recid=ftell(dfile); /* update file position */
fwrite(user1_ptr,sizeof(user1),1,dfile);
fflush(dfile);
fclose(dfile);
I can confirm the data gets appended in the file, but the value of user1.recid always returns 0 - any ideas why?
UPDATE: Looks like the issue is not all implementations require a fseek() after fopen(). I obviously need an fseek(dfile,0,SEEK_END); before I do a ftell() when appending. But if I want to read from the beginning of a text or binary file, is it also customary to place a fseek() right after fopen? Does it vary depending upon the file type?
long int ftell(FILE *stream)- chux - Reinstate Monica