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

从C语言中的文件中一次读取一个字符串,c,io,C,Io,如何一次读取一个输入字符串以调用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 usin

如何一次读取一个输入字符串以调用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);
    }
}

请尝试此选项,而不是您的扫描:

fgets (buf, sizeof (buf), fp)

您是否尝试过使用
fgets()

fgets()最多可读取大小为1个字符 并将它们存储在缓冲区中。 fgets()存储空字符 上次读取字符后的('\0') 输入缓冲区并返回“buffer” 如果一切正常,或者 错误或文件结束


scanf()
将从终端读取,因此它将挂起,等待您输入。改用
fscanf(fp,BUFFMT“s”,buf)

如何在主程序中调用这些函数?