gpt4 book ai didi

c - 从 C 语言的文件中一次读取一个字符串

转载 作者:行者123 更新时间:2023-12-02 08:59:34 34 4
gpt4 key购买 nike

如何一次读取输入一个字符串来调用 C 中的另一个函数。我认为这可行,但我的输出挂起:

#define BUFFMT "%255"
#define LINE_LEN 256
#define START_COUNT 1

// filename is declared in the main file elsewhere. I know the file opens since I tried an //old method I use to read one line at time using fgets, but I didn't know how to do one //string at a time. Thanks.
FILE *OpenFile(const char *fileName)
{
FILE *fptr;
if ((fptr = fopen(fileName, "r")) == NULL) {
fprintf(stderr, "Error opening file %s, exiting...", fileName);
exit(EXIT_FAILURE);
}
return fptr;
}

LIST *CreateList(FILE *fp)
{
char buf[LINE_LEN];

while (scanf(BUFFMT"s", buf) != EOF) {
printf("%s: \n", buf);
}
}

最佳答案

scanf()将从终端读取,因此它将挂起等待您输入输入。使用fscanf(fp, BUFFMT"s", buf)相反。

关于c - 从 C 语言的文件中一次读取一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2309131/

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