小编And*_* B.的帖子

fclrite()在fclose()之后成功

我在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;
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下输出:

Count of written objects …
Run Code Online (Sandbox Code Playgroud)

c fwrite fclose

1
推荐指数
1
解决办法
698
查看次数

标签 统计

c ×1

fclose ×1

fwrite ×1