📜  C++ vfscanf()

📅  最后修改于: 2020-09-25 08:40:19             🧑  作者: Mango

C++中的vfscanf() 函数用于从文件流中读取数据。

vfscanf() 函数在头文件中定义。

vfscanf()原型

int vfscanf(FILE* stream, const char* format, va_list vlist );

所述vfscanf() 函数从该文件读取流的数据stream通过所定义和存储的值到相应位置vlist

vfscanf()参数

vfscanf()返回值

示例:vfscanf() 函数如何工作?

#include 
#include 

void read(FILE* fp, const char * format, ... )
{
    va_list args;
    va_start (args, format);
    vfscanf (fp, format, args);
    va_end (args);
}

int main ()
{
    char myFriends[5][20] = {"Robert", "Syd", "Brian", "Eddie", "Ray"};
    FILE *fp = fopen("example.txt","w+");
    char name[20];

    for (int i=0; i<5; i++)
        fprintf(fp, "%s ", myFriends[i]);
    rewind(fp);

    printf("Here are the list of my friends\n");
    for (int i=0; i<5; i++)
    {
        read(fp, "%s ", &name);
        printf("%s\n", name);
    }
    
    fclose(fp);
    return 0;
}

运行该程序时,可能的输出为:

Here are the list of my friends
Robert
Syd
Brian
Eddie
Ray