Страницы

Поиск по вопросам

воскресенье, 15 марта 2020 г.

Не срабатывает fputs

#cpp #c #файлы #filestream


#include 
using namespace std;
int main()
{
    char tmpstr[255];
    FILE *stream;

    stream = fopen("ase.txt", "r+");

    fgets (tmpstr, 255, stream);
/*  fseek(stream, 0, SEEK_CUR);     */
    fputs("spice", stream);             // fprintf(stream, "%s","spice");  

    fclose(stream);
    return 0;
}


Почему-то не происходит запись в файл, однако, если раскомментировать строчку fseek
- запись происходит. Аналогичная ситуация, если использовать fprintf вместо fputs.
Почему так происходит?
    


Ответы

Ответ 1



Согласно стандарту С (7.21.5.3 The fopen function) 7 When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters endof-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations. Таким образом, вы не можете выполнять операцию записи в файл сразу же после операции чтения файла без промежуточного вызова одной из функций: fflush, fseek, fsetpos, или rewind

Комментариев нет:

Отправить комментарий