如何一次读取一个字符串输入以调用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);
}
}
答案 0 :(得分:4)
scanf()
将从终端读取,因此它将等待您输入您的输入。请改用fscanf(fp, BUFFMT"s", buf)
。
答案 1 :(得分:2)
试试这个而不是你的scanf:
fgets (buf, sizeof (buf), fp)
答案 2 :(得分:0)
您是否尝试过使用fgets()
fgets()读取最大为1的字符 来自流并将它们存储在缓冲区中。 fgets()存储空字符 最后一个字符读后('\ 0') 进入缓冲区并返回'缓冲区' 如果一切正常,或NULL 错误或文件结束。