Skip to content

← 返回第 15 章 套接字和标准 I/O

todes.c

c
#include <stdio.h>
#include <fcntl.h>

int main()
{
    FILE *fp;
    int fd = open("data.dat", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1)
    {
        fputs("file open error",stdout);
        return -1;
    }

    printf("First file descriptor : %d \n", fd);
    fp = fdopen(fd, "w"); //转成 file 指针
    fputs("TCP/IP SOCKET PROGRAMMING \n", fp);
    printf("Second file descriptor: %d \n", fileno(fp)); //转回文件描述符
    fclose(fp);
    return 0;
}

基于 VitePress 构建,部署于 Cloudflare Pages