c - 将文本写入 C : Ctrl + D doesn't work on Windows 中的文件

标签 c windows

我想使用 C 将文本写入文件。

这是我试过的代码:

int ecrire(){
    char buf[1024];
    int bytesStored;
    int fildes;
    char path[128];
    int i=0;
    int flags = O_WRONLY;
    printf("\n-->Donner l'emplacement du fichier : ");
    scanf("%s", path);
    fildes = ouvrir(path, flags);
    if(fildes == -1){
        return 0;
    }
    printf("\n-->Enter le contenu pour le fichier ");
    printf("(Appuyer CTRl+D a la fin du fichier) : \n");
    fflush(stdin);
    while((buf[i] = getc(stdin)) != EOF) i++;
    buf[i] = '\0';
    bytesStored = strlen(buf);
    if(write(fildes,buf,bytesStored) < 0){
        return 0;
    }
    close(fildes);
    return 1;
}

int ouvrir(char *path, int flags_t)
{
    return open(path, flags_t);
}

用户在编写文本时可以进行回车,直到他按下键盘上的 CTRL+D

在 Linux 中有效,但在 Windows 中,当我按下 CTRL+D 时,没有任何反应。

我该如何解决这个问题?

最佳答案

Ctrl + D 在 Linux 中生成 EOF(文件结尾)。在 Windows 中,您需要的键是 Ctrl + Z

关于c - 将文本写入 C : Ctrl + D doesn't work on Windows 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20773666/

相关文章:

c - 为什么编译后的代码在 linux 的 windows 中创建损坏的文件工作正常

c - 是否可以从函数返回结构体数组?

c - 使用 isdigit(x) 的意外结果

c - 函数指针执行错误

windows - 64 位应用程序的应用程序 list

python - 如何读取以 "a+"模式打开的文件?

javascript - 文本溢出省略号在 Windows (IE 10) 中不起作用

c++ - 套接字编程中如何发送长度大于缓冲区的消息?

c++ - 测试无效的窗口句柄 : should I compare with 'NULL' , '0' 甚至 'nullptr' ?

windows - Docker:卷安装目录出现 "permission denied"错误