fwrite()
ストリームを閉じた後に成功する奇妙な動作に遭遇しましたが、ファイルは失敗fclose()
として上書きされません。fflush()
私のコードは次のとおりです。
int main(int argc, char* argv[])
{
FILE* file = fopen("file.txt", "w");
if(!file) perror("Cannot open the file.\n");
char text[] = "1234567";
fclose(file);
int count_of_written_objects = fwrite(text, sizeof(text),1, file);
printf("Count of written objects into the file: %d \n", count_of_written_objects);
if(count_of_written_objects != 1) perror("Not expected count of objects was written.\n");
int success = fflush(file);
printf("Variable success: %d \n", success);
if(success == EOF) perror("Flush did not succeed. \n");
return 0;
}
次の出力が得られます。
Count of written objects into the file: 1
Variable success: -1
Flush did not succeed.
: Bad file descriptor
fwrite()
ストリームが閉じられたときにどのように成功できますか? fwrite()
閉じたストリームに書き込みできますか? 説明していただけますか?