将文件内容读写到结构体中问题
程序代码:
int browse_stu_xinxi() { char buf[1024]; FILE *fp; //char *str123; int i = 0; int n= 0; memset(buf, 0x00, sizeof(buf)); fp = fopen("F:\stuinfo.txt", "r"); if (fp == NULL) { printf("文件打开错误\n"); exit(0); } //printf("%s",students[i].address); while (fgets(buf, 1024, fp)) { //n = strlen(buf); //fseek(fp, n + 1, SEEK_SET); printf("%s\n", buf); /* 思考 用strtok str = strtok(buf, "\t"); while (str!=NULL) { students[i].stu_no = str; str = strtok(NULL, "\t"); } */ //有问题,运行就有问题,还有思考,在已经有文件内容继续添加数据。 printf("这是第%d条数据\n", i); fscanf(fp, "%s %s %s %s %s %s %s %s", &students[i].stu_no, &students[i].name, &students[i].age, &students[i].sex, &students[i].birth_date, &students[i].address,&students[i].phone, &students[i].email); printf("%s,%s,%s,%s,%s,%s,%s,%s,%d\n", students[i].stu_no, students[i].name, students[i].age, students[i].sex, students[i].birth_date, students[i].phone, students[i].address, students[i].email,i); i++; memset(buf, 0x00, sizeof(buf)); } /* char buf[1024]; scanf("%s",buf); printf("%d\n",sizeof()); */ fclose(fp); return 0; }