该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
struct student
{
int phone[12];
int grade[4];
int sno[11];
char sname[8];
char sex[5];
};
int main()
{
struct student *v = NULL;
FILE *fp=fopen("G://new.txt","r");
if(fp==NULL)
{
printf("文本打开错误/n");
return -1;
}
int i=0;
char c[9999];
v = (struct student*)malloc(sizeof(struct student));
while(!feof(fp))//文件指针还未到达文件末尾时,输出文件中数据//原理说明:feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为非零值,否则为0
{
fscanf(fp,"%[^,]%",&c);
//printf("%d/n",c);
}
char * split = ",";
char * p;
char *a[9999];
int j=0,k;
p = strtok (c,split);
while(p!=NULL)
{
//printf ("%s\n",p);
a[j]=p;j++;
p = strtok(NULL,split); //用逗号分割数据,并把数据存入指针数组a中
}
//for(j=0;j<10;j++)
//printf("%s\n",a[j]);
for(i=0;i<4;i++)
{
strcpy(v[i].sno,a[i*5]);
strcpy(v[i].sname,a[5*i+1]);
strcpy(v[i].sex,a[5*i+2]);
strcpy(v[i].phone,a[5*i+3]);
strcpy(v[i].grade,a[5*i+4]);
printf("学号:%s,姓名:%s,性别:%s,联系方式:%s,成绩:%s\n",v[i].sno,v[i].sname,v[i].sex,v[i].phone,v[i].grade);
// printf("%s",v[i].sno);
}
fclose(fp);
system("pause");
return 0;
}