Код затирает открываемы файлы

Модератор: Модераторы разделов

Ответить
_Gleb_
Сообщения: 467
ОС: Kubuntu 12.04 LTS

Код затирает открываемы файлы

Сообщение _Gleb_ »

По моему разумению, этот код должен выводить на экран название вводимого файла и его 18-й байт. Но выходит так, что он опустошает файл.

Код: Выделить всё

#include <stdio.h>
main(int argc, char *argv[]){
  FILE *bfile;
  bfile = fopen(argv[1], "w");
  printf("%s%s", argv[1], "\n");
  fseek(bfile, 18, 0);
  char ltt;
  ltt = getc(bfile);
  printf("\n", ltt);
  fclose(bfile);
}

Подскажите, что я делаю неправильно!
Изображение
Спасибо сказали:
Аватара пользователя
edoc_modnar
Бывший модератор
Сообщения: 1638
Статус: Форум больше не посещаю

Re: Код затирает открываемы файлы

Сообщение edoc_modnar »

Код: Выделить всё

bfile = fopen(argv[1], "w");


"w" - неправильно, пробуй "a". man fopen.
So long, and thanks for all the fish.
Douglas Adams, The Hitchhiker's Guide to the Galaxy
Спасибо сказали:
Аватара пользователя
Uncle_Theodore
Сообщения: 3339
ОС: Slackware 12.2, ArchLinux 64

Re: Код затирает открываемы файлы

Сообщение Uncle_Theodore »

Открой man fopen и почитай

The argument mode points to a string beginning with one of the follow-
ing sequences (Additional characters may follow these sequences.):


r Open text file for reading. The stream is positioned at the
beginning of the file.


r+ Open for reading and writing. The stream is positioned at the
beginning of the file.

w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.


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.
Спасибо сказали:
Ответить