gpt4 book ai didi

c - 直接在终端上读取

转载 作者:行者123 更新时间:2023-11-30 20:40:32 24 4
gpt4 key购买 nike

我正在尝试从终端写入文件,直到您写入“退出”。该代码可以复制,但不起作用。它甚至不写文件!我该怎么办?

void            my_script(char* name, t_Option option)
{
FILE *file;

(void)option;
while (strcmp("/stdin", "exit") != 0)
{
file = NULL;
file = fopen(name,"w");
fprintf(file, "%s", "Writing in a file !\n");
fclose(file);
}
}

坦克来帮忙! :)

/************ *******/

这是我的解决方案,向你们致敬,他们提醒了我很多;)

void            my_script(char* name)
{
FILE *file;
char buff[4096];
int len;

file = NULL;
len = 0;
file = fopen(name,"w");
while ((len = read(0, buff, 4096)) != -1)
{
buff[len] = '\0';
if (strncmp(buff, "exit", 4) == 0)
break;
fprintf(file, "%s", buff);
}
fclose(file);
}

最佳答案

FILE *file = fopen(name, "w");
char buff[BUFSIZ];
while(fgets(buff, sizeof(buff), stdin) && strcmp(buff, "exit\n")){
fprintf(file, "%s", buff);
}
fclose(file);

关于c - 直接在终端上读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com