Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > дописать в файл


Автор: meta 28.10.2007, 13:00
Доброй утра всем  smile  По причине не знания особенностей си, не пойму как реализовать дописание в файл. Я его открываю на запись и пишу туда... почему то он переписывает его :( вместо того чтобы дописать в конец.
Код

int file = open("moidodir_journal.bin", O_WRONLY | O_BINARY  , S_WRITE);
write( file, &i, sizeof(int));

Автор: JackYF 28.10.2007, 15:01
meta, эээ...

Код

FILE* f = fopen("moidodir_journal.bin", "bw+");
fwrite(&i, sizeof(int), f);

Автор: Mayk 28.10.2007, 15:11
Цитата(JackYF @  28.10.2007,  19:01 Найти цитируемый пост)
FILE* f = fopen("moidodir_journal.bin", "bw+");
fwrite(&i, sizeof(int), f);

не совсем.
Цитата(man fopen)

      w+     Open for reading and writing.  The file is created  if  it  does
              not  exist, otherwise it is truncated.  The stream is positioned
              at the beginning of the file.

       a      Open for appending (writing at end of file).  The file  is  cre‐
              ated  if it does not exist.  The stream is positioned at the end
              of the file.


Цитата(meta @  28.10.2007,  17:00 Найти цитируемый пост)
int file = open("moidodir_journal.bin", O_WRONLY | O_BINARY  , S_WRITE);

Цитата(man 2 open)

     O_APPEND
              The file is opened in append mode. Before each write(), the file
              offset is positioned at the end of the file, as if with lseek().
              O_APPEND may lead to corrupted files on NFS file systems if more
              than one process appends data  to  a  file  at  once.   This  is
              because  NFS does not support appending to a file, so the client
              kernel has to simulate it, which can’t be done  without  a  race
              condition.


Автор: meta 28.10.2007, 17:31
Спасибо, буду использовать APPEND. 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)