检查int read(struct person p[]) { FILE *fp; int count=0; fp=fopen("person.txt",'r'); if(fp==NULL){ printf("Failed to open file.\n"); return 0; } while(fscanf(fp,"%s%s",p[count].ID,p[count].name==2){ count++; } fclose(fp); return count; }
时间: 2023-06-24 16:08:45 浏览: 71
在你的代码中,有一些问题:
1. `fopen` 函数的第二个参数应该是一个字符串,而你传递的是一个字符常量,应该使用双引号,而不是单引号。
2. `fscanf` 函数的第二个参数有误,你的意图是使用等于号将两个字符串的读取结果赋值给结构体中的两个成员变量,但是这是错误的语法。你应该使用逗号将多个参数分隔开,例如 `fscanf(fp, "%s %s", p[count].ID, p[count].name)`。
下面是修改后的代码:
```
int read(struct person p[]) {
FILE *fp;
int count = 0;
fp = fopen("person.txt", "r");
if (fp == NULL) {
printf("Failed to open file.\n");
return 0;
}
while (fscanf(fp, "%s %s", p[count].ID, p[count].name) == 2) {
count++;
}
fclose(fp);
return count;
}
```
另外,你可能需要检查 `person.txt` 文件是否存在,并且文件中每行的格式是否正确,以免导致程序出现意外错误。
相关问题
用c编写一个,#include <stdio.h> #include <string.h> #define N 100 struct birth { int year; int month; int day; }; struct person { char ID[19]; char name[20]; struct birth birthday; int flag;//校验位是否正确, 1代表正确,0代表错误 }; int read(struct person p[]) { FILE *fp; int count=0; fp=fopen("person.txt",'r'); if(fp==NULL){ printf("Failed to open file.\n"); return -1; } while(fscanf(fp,"%s%s%d%d%d%d",p[count].ID,p[count].name, &p[count].birthday.year,&p[count].birthday.month, &p[count].birthday.day,&p[count].flag)!=EOF){ count++; } fclose(fp); return count; } int checkID(char *ID) { int weight[18]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};//ID号码权重 char checkcode[11]={1,0,'X',9,8,7,6,5,4,3,2};//校验码字符 int sum=0;//加权和 int i; if(strlen(ID)!=18){ return 0; } for(i=0;i<17;i++){ if(!isdigit(ID[i])){//isdigit=? return 0; } } for(i=0;i<17;i++){ // sum+=ID[i]*weight[i]; sum+=(ID[i]-'0')*weight[i]; } int code; code=sum%11; if(ID[17]==checkcode[code]){ return 1; } else{ return 0; } } struct birth get_birth(char *ID) { int i,j; struct birth b; char year_s[5]={0}, month_s[3]={0}, day_s[3]={0}; for(i=6,j=0;j<4;i++,j++){ year_s[j]=ID[i]; } for(i=10,j=0;j<2;i++,j++){ month_s[j]=ID[i]; } for(i=12,j=0;j<2;i++,j++){ day_s[j]=ID[i]; } //将字符串转换为整型 b.year=(year_s[0]-'0')*1000+(year_s[1]-'0')*100+ (year_s[2]-'0')*10+(year_s[3]-'0'); b.month=(month_s[0]-'0')*10+(month_s[1]-'0'); b.day=(day_s[0]-'0')*10+(day_s[1]-'0'); return b; } void get_all_person(struct person p[], int n) { int i; for(i=0;i<n;i++){ struct birth b=get_birth(p[i].ID); if(checkID(p[i].ID)){ p[i].birthday=b; p[i].flag=1; } else{ p[i].flag=0; } } }void save(struct person p[],int n); 将身份证号码正确的数据(身份证号码、姓名、出生年月日)写入文件person_checked.txt的函数。其中数组 p 中存放 n 个公民信息。
void save(struct person p[], int n) {
FILE *fp;
fp = fopen("person_checked.txt", "w");
if (fp == NULL) {
printf("Failed to open file.\n");
return;
}
int i;
for (i = 0; i < n; i++) {
if (p[i].flag == 1) {
fprintf(fp, "%s %s %d-%02d-%02d\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day);
}
}
fclose(fp);
}
用c编写,程序开始时,自动从文件读入信息到结构体数组,并提取每个公民的出生年月放入结构体中,同时进行身份证校验位是否正确,判断信息也放入结构体中。用户选择1时,显示校验位正确的公民信息;用户选择1时,显示校验位正确的公民信息;用户选择2时,显示校验位错误的公民信息,并统计人数。用户选择2时,显示校验位错误的公民信息,并统计人数。用户选择3时,将正确公民信息按出生日期排序并输出。 若用户选择0,会退出程序,但在此之前,会自动将身份证号码正确的数据写入文件person_checked.txt中。补全主函数,#include <stdio.h> #include <string.h> #define N 100 struct birth { int year; int month; int day; }; struct person { char ID[19]; char name[20]; struct birth birthday; int flag;//校验位是否正确, 1代表正确,0代表错误 }; int read(struct person p[]); int checkID(char *ID); struct birth get_birth(char *ID); void get_all_person(struct person p[], int n); void display_person(struct person p[], int n, int flag); int birth_cmp(struct birth birth1, struct birth birth2); void birth_sort(struct person p[], int n); int search(struct person p[], int n, char *name); void save(struct person p[], int n); int prompt(void); int main(void) { struct person p[N]; int cmd, n; while (1) { cmd = prompt(); } return 0; }
补全代码如下:
```c
#include <stdio.h>
#include <string.h>
#define N 100
struct birth {
int year;
int month;
int day;
};
struct person {
char ID[19];
char name[20];
struct birth birthday;
int flag;//校验位是否正确, 1代表正确,0代表错误
};
int read(struct person p[]);
int checkID(char *ID);
struct birth get_birth(char *ID);
void get_all_person(struct person p[], int n);
void display_person(struct person p[], int n, int flag);
int birth_cmp(struct birth birth1, struct birth birth2);
void birth_sort(struct person p[], int n);
int search(struct person p[], int n, char *name);
void save(struct person p[], int n);
int prompt(void);
int main(void) {
struct person p[N];
int cmd, n;
n = read(p);
while (1) {
cmd = prompt();
switch (cmd) {
case 0:
save(p, n);
return 0;
case 1:
get_all_person(p, n);
display_person(p, n, 1);
break;
case 2:
get_all_person(p, n);
display_person(p, n, 0);
break;
case 3:
get_all_person(p, n);
birth_sort(p, n);
display_person(p, n, 1);
break;
default:
printf("Invalid command\n");
}
}
return 0;
}
int read(struct person p[]) {
FILE *fp;
char filename[100];
int i = 0;
printf("Enter the filename: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
return 0;
}
while (fscanf(fp, "%s %s", p[i].ID, p[i].name) == 2) {
if (checkID(p[i].ID)) {
p[i].flag = 1;
p[i].birthday = get_birth(p[i].ID);
i++;
}
else {
p[i].flag = 0;
}
}
fclose(fp);
return i;
}
int checkID(char *ID) {
int i, sum = 0;
int weight[18] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char check[11] = "10X98765432";
for (i = 0; i < 17; i++) {
if (ID[i] < '0' || ID[i] > '9') {
return 0;
}
sum += (ID[i] - '0') * weight[i];
}
if (ID[17] == check[sum % 11]) {
return 1;
}
return 0;
}
struct birth get_birth(char *ID) {
struct birth birth;
birth.year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '0');
birth.month = (ID[10] - '0') * 10 + (ID[11] - '0');
birth.day = (ID[12] - '0') * 10 + (ID[13] - '0');
return birth;
}
void get_all_person(struct person p[], int n) {
int i;
for (i = 0; i < n; i++) {
if (p[i].flag == 0) {
p[i].birthday.year = 0;
p[i].birthday.month = 0;
p[i].birthday.day = 0;
}
else {
p[i].birthday = get_birth(p[i].ID);
}
}
}
void display_person(struct person p[], int n, int flag) {
int i, count = 0;
printf("ID\t\t\tName\t\t\tBirthday\n");
for (i = 0; i < n; i++) {
if (p[i].flag == flag) {
count++;
printf("%s\t%s\t%d-%02d-%02d\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day);
}
}
if (count == 0) {
printf("No data found\n");
}
else {
printf("Total: %d\n", count);
}
}
int birth_cmp(struct birth birth1, struct birth birth2) {
if (birth1.year != birth2.year) {
return birth1.year - birth2.year;
}
else if (birth1.month != birth2.month) {
return birth1.month - birth2.month;
}
else {
return birth1.day - birth2.day;
}
}
void birth_sort(struct person p[], int n) {
int i, j;
struct person tmp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (birth_cmp(p[i].birthday, p[j].birthday) > 0) {
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
}
int search(struct person p[], int n, char *name) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(p[i].name, name) == 0) {
return i;
}
}
return -1;
}
void save(struct person p[], int n) {
FILE *fp;
int i;
fp = fopen("person_checked.txt", "w");
if (fp == NULL) {
printf("Failed to open file person_checked.txt\n");
return;
}
for (i = 0; i < n; i++) {
if (p[i].flag == 1) {
fprintf(fp, "%s %s\n", p[i].ID, p[i].name);
}
}
fclose(fp);
}
int prompt(void) {
int cmd;
printf("\n");
printf("0. Exit\n");
printf("1. Show all valid persons\n");
printf("2. Show all invalid persons\n");
printf("3. Sort persons by birth date\n");
printf("Enter command: ");
scanf("%d", &cmd);
return cmd;
}
```
阅读全文