In my file I need to store the following (mainly for a small
database):
int position;
char *data;
int check;
Also I have to add the data mentioned at EOF
Open the file in append mode. See the second parameter to fopen().
Choose whether you want:
- non-portable binary files (easy to read and write - just use fwrite()
and fread(), but be careful with that pointer - but only guaranteed to
be useable on the machine they were written on);
- portable text files (relatively easy to read and write using printf(),
fgets() and sscanf(), and if done right pointers are no problem, but
they can be relatively bulky and easy to tamper with);
- or whether you want to devise a portable binary format or choose a
portable binary format someone else invented, which can be somewhat
more complex to read and write (typically using functions which
arrange your struct members in the right byte order, and then call
fwrite(), and v.v. as well, of course), but can also be both portable
and compact.
Which of these options is right for you depends entirely on your
situation, and what the program is supposed to do. However, you might
get some inspiration at said:
or sometimes randomly at any position in the file, or delete at
any position. Can this be done.
Under most popular OSes, this cannot be done in any language. It is not
a problem of C itself. Inserting or removing bytes in the middle of a
file typically involves copying it. Most databases don't actually delete
records; they mark them with a flag saying "this record has been
deleted", and either re-use them or remove them all in one go when you
re-index the database.
If you'd been a good Usenetter and read the FAQ, you'd have known this,
btw: <
http://www.eskimo.com/~scs/C-faq/q19.14.html>.
Richard