--}}
Новая тема
Вы не можете создавать новые темы.
Т.к. вы неавторизованы на сайте. Пожалуйста назовите себя или зарегистрируйтесь.
Список тем

fopen ("filename", "a") - в каких случаях фунция возвращает NULL?

42
5
С друзьями на NN.RU
В социальных сетях
Поделиться
theftp
16.03.2008
void func()
{
...
FILE* fp = fopen("filename", "a");
fprintf(fp, ...data...);
fclose(fp);
...
}

Эта функция вызывается много раз и всё сначала нормально. но вдруг БАЦ! ошибка. Почему? не пойму. Раньше всё работало.
Header File

stdio.h

Category

Input/output Routines

Syntax

#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
FILE *_wfopen(const wchar_t *filename, const wchar_t *mode);

Description

Opens a stream.

fopen opens the file named by filename and associates a stream with it. fopen returns a pointer to be used to identify the stream in subsequent operations.

The mode string used in calls to fopen is one of the following values:

Value Description

r Open for reading only.
w Create for writing. If a file by that name already exists, it will be overwritten.
a Append; open for writing at end-of-file or create for writing if the file does not exist.
r+ Open an existing file for update (reading and writing).
w+ Create a new file for update (reading and writing). If a file by that name already exists, it will be overwritten.
a+ Open for append; open (or create if the file does not exist) for update at the end of the file.

To specify that a given file is being opened or created in text mode append a t to the mode string (rt w+t and so on). Similarly to specify binary mode append a b to the mode string (wb a+b and so on). fopen also allows the t or b to be inserted between the letter and the + character in the mode string; for example rt+ is equivalent to r+t.

If a t or b is not given in the mode string the mode is governed by the global variable _fmode. If _fmode is set to O_BINARY files are opened in binary mode. If _fmode is set to O_TEXT they are opened in text mode. These O_... constants are defined in fcntl.h.

When a file is opened for update, both input and output can be done on the resulting stream; however,

output cannot be directly followed by input without an intervening fseek or rewind
input cannot be directly followed by output without an intervening fseek, rewind, or an input that encounters end-of-file

Return Value

On successful completion fopen returns a pointer to the newly opened stream. In the event of error it returns NULL.
theftp
16.03.2008
In the event of error

And what kinds of error events exist in case of "a" mode?
Stinky
16.03.2008
Точнее:
Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer. Otherwise, NULL is returned
and the global variable errno is set to indicate the error.

Как использовать сам придумаешь?
theftp
16.03.2008
Чёрт! совсем забыл об этой переменной.
Спасибо, ошибку нашёл: errno=EMFILE (Too many open files. No more file descriptors are available, so no more files can be opened).
В другой похожей функции вместо fclose вызывалась fflush. Надо же...
Новая тема
Вы не можете создавать новые темы.
Т.к. вы неавторизованы на сайте. Пожалуйста назовите себя или зарегистрируйтесь.
Список тем
Последние темы форумов
Форум Тема (Автор) Последний ответ Ответов
Сетевой фильтр APC Surge Arrest

Сетевой фильтр APC Surge Arrest для радиолюбителя.и не только Отправка в регионы после оплаты. ЦЕНА 3000 руб. В рабочем состоянии....
Цена: 3 000 руб.

Оперативная память Corsair XMS3 CMX8GX3M2A1600C9

Оперативная память Corsair XMS3 CMX8GX3M2A1600C9 Отправка в регионы после оплаты. Продаются сразу обе. Цена за обе 2000 руб....
Цена: 1 000 руб.

Принтер лазерный HEWLETT PACKARD HP-6L

Принтер лазерный HEWLETT PACKARD HP-6L Отправка в регионы после оплаты. 3штуки БУ. Внешний вид из магазина простояли на складе...
Цена: 4 500 руб.

Материнские платы на запчасти и не только

Материнские платы на запчасти и не только Материнские платы и другие комплектующие Отправка в регионы после оплаты. Транспортной...
Цена: 3 000 руб.