[C] 纯文本查看 复制代码
struct address{
char name[20];
int age;
char cla[30];
char tele[20];
};
void save(address t[], int n){
int i;
FILE *fp;
if ((fp = fopen("c:\\vc\\4-8.txt", "wb")) == NULL){ //改
printf("can not open this file");
exit(1);
}
printf("\nSaving file\n");
fprintf(fp, "%d", n);
fprintf(fp, "\r\n");
for (i = 0; i < n; i++){
fprintf(fp, "%s,%d,%s,%s", t[i].name, t[i].age, t[i].cla, t[i].tele); //改
fprintf(fp, "\r\n");
}
fclose(fp);
printf("****保存成功****");
}
int load(address t[]){
int i, n;
FILE *fp;
if ((fp = fopen("c:\\vc\\4-8.txt", "rb")) == NULL){ //改
printf("can not open this file");
exit(1);
}
fscanf(fp, "%d", &n);
for (i = 0; i < n; i++){
fscanf(fp, "\r\n%[^,],%d,%[^,],%s", t[i].name, &t[i].age, t[i].cla, t[i].tele); //改
}
fclose(fp);
printf("****读取成功****");
return n;
}
void list(address t[], int n){
int i;
printf("\n**********address********\n");
printf("name\t\tage\t\tclass\t\ttelephone\n");
printf("-----------------------------------------------\n");
for (i = 0; i < n; i++){
printf("%-20s%-20d%-20s%10s\n", t[i].name, t[i].age, t[i].cla, t[i].tele); //改
if ((i + 1) % 10 == 0){
printf("请按任意键继续");
getch();
}
}
}
int main(){
char i;
struct address r[4], s[4] = { //改
{ "刘嘉兴", 20, "软件32", "10010" },
{ "张素生", 20, "软件32", "10010" },
{ "潘续旭", 20, "软件32", "10086" },
{ "杜文权", 20, "软件32", "10011" },
};
save(s, 4);
i = load(r);
list(r, i);
return 0;
}