void write(student *head){ FILE *fp = fopen("duli16.3.dat", "w"); student *p = head->next; while (p){ fprintf(fp,"%s %s %f\n",p->num,p->num,p->money); p = p->next; } fclose(fp); }
时间: 2024-02-02 09:04:57 浏览: 32
好的,下面是添加了注释的代码:
```c
void write(student *head){
FILE *fp = fopen("duli16.3.dat", "w"); // 打开文件,如果打开失败则返回
if (!fp) {
printf("File open error!\n");
return;
}
student *p = head->next; // 指向链表头节点的下一个节点
// 遍历链表,将每个节点的数据写入文件中
while (p){
fprintf(fp,"%s %s %f\n",p->num,p->num,p->money);
p = p->next;
}
fclose(fp); // 关闭文件
}
```
注释中解释了每一行代码的作用,方便了解代码的功能和流程。
相关问题
void lianbiao(struct student *head) { FILE *fp; struct student *p1, *q1; q1 = head; if ((fp = fopen("duli16.3.dat", "r")) == NULL) { printf("File open error!\n"); return; } while (1) { p1 = (struct student *)malloc(sizeof(struct student)); if((fscanf(fp, "%s %s %lf", p1->num, p1->name, &p1->money)) ==-1){ break; }; // printf("%s\n",p1->num); p1->next = NULL; q1->next = p1; q1 = p1; } q1->next = NULL; fclose(fp); // printf("111\n"); }
lianbiao函数的作用是从文件"duli16.3.dat"中读取学生信息,存储到链表中。具体实现代码如下:
```c
void lianbiao(struct student *head) {
FILE *fp;
struct student *p1, *q1;
q1 = head;
if ((fp = fopen("duli16.3.dat", "r")) == NULL) {
printf("File open error!\n");
return;
}
while (1) {
p1 = (struct student *)malloc(sizeof(struct student));
if((fscanf(fp, "%s %s %lf", p1->num, p1->name, &p1->money)) ==-1){
break;
};
p1->next = NULL;
q1->next = p1;
q1 = p1;
}
q1->next = NULL;
fclose(fp);
}
```
dayin函数的作用是遍历链表,输出学生信息。具体实现代码如下:
```c
void dayin(struct student *head) {
struct student *p;
p = head->next;
while (p != NULL) {
printf("%s %s %lf\n", p->num, p->name, p->money);
p = p->next;
}
}
```
FILE* fp = fopen("./file", "r");什么意思
当你在C语言中看到这样的代码 `FILE* fp = fopen("./file", "r");`,它的意思是:
1. `FILE*` 是一个指针类型,用来指向 `FILE` 结构体,这是C标准库中的一个类型,用于文件操作。
2. `fopen()` 是一个标准库函数,全称为 "file open",用于打开一个文件。它接受两个参数:
- 第一个参数 `fp` 是一个 `FILE*` 指针,将在函数执行后被赋值为打开的文件句柄。
- 第二个参数是一个字符串,是打开模式,这里 `"r"` 表示以读取(read)模式打开文件。
3. `"./file"` 是一个路径名,`.` 表示当前目录,`file` 是文件名。这行代码是在当前目录下尝试打开名为 `file` 的文本文件。
4. 如果文件成功打开,`fopen()` 将返回一个指向文件的非空 `FILE*`,表示文件打开成功;如果失败,则会返回 `NULL`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)