写进去几个,读回来几个

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

(本条路线统一用 gcc -std=c11 -O0 -Wall 编译)自己写、自己读,三个回执一起看:

#include <stdio.h>
#include <string.h>

int main(void) {
    /* tmpfile:没有名字、用完自动删,判题机并行跑也不会撞 */
    FILE *f = tmpfile();
    if (f == NULL) { printf("建不出\n"); return 1; }

    const char *msg = "hello";
    size_t w = fwrite(msg, 1, strlen(msg), f);

    rewind(f);
    char buf[16] = {0};
    size_t r = fread(buf, 1, sizeof(buf) - 1, f);
    fclose(f);

    /* 三个回执:写了几个、读了几个、内容对不对得上 */
    printf("%zu/%zu/%d\n", w, r, strcmp(buf, msg) == 0);
    return 0;
}
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论