c语言实现读取txt文件内容到结构体数组中

#include 
#include 

#define N 10
typedef struct 
{
    char work_ID[5];
    char name[20];
    char phone_nu[12];
}student;


int main(int argc, char *argv[])
{
    student st[N];
    FILE *fp;
    int i;
    int count;

    if(argc != 2)
    {
        fprintf(stderr, "usage:argc is not two\n");
        exit(1);
    }

    if((fp = fopen(argv[1], "rb")) == NULL)
    {
        fprintf(stderr, "Can't open the %s", argv[1]);
    }
    
    for(i = 0; i < N; i++)
    {
        if((fscanf(fp, "%s%s%s", st[i].work_ID, st[i].name, st[i].phone_nu)) != 3)
        {
            break;
        }
    }
    
    count = i;
    //display
    printf("the ture count is %d\n",count);
    for(i = 0; i < count; i++)
    {
        printf("%s\t%s\t%s\n", st[i].work_ID, st[i].name, st[i].phone_nu);
    }
    
    return 0;
}


 

你可能感兴趣的:(C语言)