struct xinxi *duqu2() { struct xinxi *head2= NULL; struct xinxi *temp = NULL; char dianhua[20], mima[20],chepaihao[20]; FILE *fp = fopen("用户信息.txt", "r"); if (fp == NULL) { printf("\t\t\t\t打开文件失败!文件名: 用户信息.txt\n"); return NULL; } else { while (fscanf(fp, "%s %s %s", dianhua, mima,chepaihao) != EOF) { struct xinxi *p = (struct xinxi *)malloc(sizeof(struct xinxi)); if (p == NULL) { printf("分配内存失败!\n"); } strcpy(p->dianhua, dianhua); strcpy(p->mima, mima); strcpy(p->chepaihao,chepaihao); if (head2 == NULL) { head2 = temp = p; } else { temp->next = p; temp = p; } } fclose(fp); if (temp != NULL) { temp->next = NULL; } return head2; } } void chongzhi() { char dianhua[N]; char mima[N]; char xinmima[N]; struct xinxi *head2,*p,*q; p=head2=duqu2(); printf("请输入账号(联系方式):"); scanf("%s",dianhua); printf("请输入原密码:"); scanf("%s",mima); while(p!=NULL) { if(strcmp(dianhua,p->dianhua)==0&&strcmp(mima,p->mima)==0) { printf("请输入新密码:"); scanf("%s",xinmima); strcpy(p->mima,xinmima); break; } p=p->next; } if(p==NULL) { printf("\t\t\t\t账号(联系方式)或密码错误!\n"); } else { FILE *fp; fp=fopen("\t\t\t\t用户信息.txt","w"); q=head2; while(q!=NULL) { fprintf(fp,"%s %s %s\n", q->dianhua, q->mima,q->chepaihao); q=q->next; } fclose(fp); printf("\t\t\t\t重置成功!\n"); } }为什么错了
时间: 2024-04-28 22:26:36 浏览: 17
在 `chongzhi()` 函数中,`head2` 指针没有被初始化,而是直接调用了 `duqu2()` 函数返回的空指针。因此,在 `head2` 指针未初始化的情况下,对其进行操作会导致程序崩溃或者出现意料之外的结果。
你需要在 `chongzhi()` 函数的开头,使用 `duqu2()` 函数初始化 `head2` 指针,即将 `struct xinxi *head2=NULL;` 修改为 `struct xinxi *head2=duqu2();`,这样才能对 `head2` 指针进行操作。
相关问题
struct xinxi *duqu2() { struct xinxi *head2= NULL; struct xinxi *temp = NULL; char dianhua[20], mima[20],chepaihao[20]; FILE *fp = fopen("用户信息.txt", "ab+"); if (fp == NULL) { printf("\t\t\t\t打开文件失败!文件名: 用户信息.txt\n"); return NULL; } else { while (fscanf(fp, "%s %s %s", dianhua, mima,chepaihao) != EOF) { struct xinxi *p = (struct xinxi *)malloc(sizeof(struct xinxi)); if (p == NULL) { printf("分配内存失败!\n"); } strcpy(p->dianhua, dianhua); strcpy(p->mima, mima); strcpy(p->chepaihao,chepaihao); if (head2 == NULL) { head2 = temp = p; } else { temp->next = p; temp = p; } } fclose(fp); if (temp != NULL) { temp->next = NULL; } return head2; } }优化
可以对这段代码进行如下优化:
1. 增加错误处理逻辑,当文件打开失败或读取数据失败时,应该及时关闭文件并释放内存。
2. 使用fgets函数读取文件内容,避免使用%s格式符造成的数据读取不正确的问题。
3. 在读取文件内容时,对读取到的数据进行有效性检查,避免出现异常情况。
4. 在动态分配内存时,对内存分配结果进行有效性检查,避免分配失败导致的程序异常。
5. 在链表的构建过程中,增加错误处理逻辑,避免链表出现异常。
优化后的代码如下:
```
struct xinxi *duqu2() {
struct xinxi *head2 = NULL;
struct xinxi *temp = NULL;
char line[64];
char dianhua[20], mima[20], chepaihao[20];
FILE *fp = fopen("用户信息.txt", "r");
if (fp == NULL) {
printf("打开文件失败!文件名: 用户信息.txt\n");
return NULL;
}
while (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "%s %s %s", dianhua, mima, chepaihao) != 3) {
printf("读取文件内容失败!\n");
fclose(fp);
return NULL;
}
struct xinxi *p = (struct xinxi *)malloc(sizeof(struct xinxi));
if (p == NULL) {
printf("分配内存失败!\n");
fclose(fp);
return NULL;
}
strcpy(p->dianhua, dianhua);
strcpy(p->mima, mima);
strcpy(p->chepaihao, chepaihao);
if (head2 == NULL) {
head2 = temp = p;
} else {
temp->next = p;
temp = p;
}
}
fclose(fp);
if (temp != NULL) {
temp->next = NULL;
}
return head2;
}
```
struct Troom *p, *pl, *tempp, *head=NULL
这段代码定义了四个指向结构体Troom的指针变量:p、pl、tempp和head。其中,head被初始化为NULL。
struct Troom是一个结构体类型,可能包含了一些成员变量和成员函数。通过指针变量p、pl、tempp和head,可以操作和访问这个结构体类型的对象。
相关问题:
1. 什么是结构体?
2. 如何定义一个结构体类型?
3. 如何使用指针变量来操作结构体对象?
4. 为什么要将head初始化为NULL?
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)