fprintf中的%-15s
时间: 2024-06-03 17:09:19 浏览: 12
As an AI language model, I am programmed to understand the concept of "null" as a value that represents the absence of a meaningful or valid data value. In programming languages, "null" is often used to indicate the absence of an object, variable or value. It is also commonly used to indicate an empty or undefined state.
相关问题
#include <stdio.h> #include <stdlib.h> struct telnote { int no; char name[20]; char tel[20]; }; int main(void) { int code, i, cnt = 0; char scode[10]; FILE *fp; struct telnote mytel[10], buff, * p; p = &buff; while (1) { printf("\nコード入力:"); gets_s(scode); code = atoi(scode); if (code <= 0)break; p->no = code; printf("氏名入力:"); gets_s(p->name); printf("電話番号入力:"); gets_s(p->tel); mytel[cnt] = ;//补充完成 ++cnt; } if (cnt > 0) { fp = fopen("mytel.data", "w"); if (fp == NULL) { printf("file open error!!\n"); exit(1); } printf("\nコード氏名 電話番号\n"); for (i = 0; i < cnt; i++) { printf("%5d %-15s %-15s\n", buff.no, buff.name, buff.tel); fprintf();//补充完成 } fclose(fp); } else { printf("\nデータが入力されていません\n"); } return 0; }把缺失的补充完成
在代码中,需要补充完成以下两处:
1. 在while循环中,将输入的数据存入数组中。
```c
while (1) {
printf("\nコード入力:");
gets_s(scode);
code = atoi(scode);
if (code <= 0)break;
p->no = code;
printf("氏名入力:");
gets_s(p->name);
printf("電話番号入力:");
gets_s(p->tel);
mytel[cnt] = buff; // 将输入的数据存入数组中
++cnt;
}
```
2. 在for循环中,将数据写入文件中。
```c
for (i = 0; i < cnt; i++) {
printf("%5d %-15s %-15s\n", mytel[i].no, mytel[i].name, mytel[i].tel);
fprintf(fp, "%d %s %s\n", mytel[i].no, mytel[i].name, mytel[i].tel); // 将数据写入文件中
}
```
#include <stdio.h> #include <stdlib.h> struct telnote { int no; char name[20]; char tel[20]; }; int main(void) { int code, i, cnt = 0; char scode[10]; FILE *fp; struct telnote mytel[10], buff, * p; p = &buff; while (1) { printf("\nコード入力:"); gets_s(scode); code = atoi(scode); if (code <= 0)break; p->no = code; printf("氏名入力:"); gets_s(p->name); printf("電話番号入力:"); gets_s(p->tel); mytel[cnt] =buff ; ++cnt; } if (cnt > 0) { fp = fopen("mytel.data", "w"); if (fp == NULL) { printf("file open error!!\n"); exit(1); } printf("\nコード氏名 電話番号\n"); for (i = 0; i < cnt; i++) { printf("%5d %-15s %-15s\n", buff.no, buff.name, buff.tel); fprintf(fp, "%d %s %s\n", mytel[i].no, mytel[i].name, mytel[i].tel); } fclose(fp); } else { printf("\nデータが入力されていません\n"); } return 0; }使代码可以正常运行
首先需要将第一行和第二行之间加上一个空行,否则编译器会将它们当成一个行。
然后,需要将`gets_s`替换为`fgets`,因为`gets_s`在某些编译器上可能会报错,而且`gets_s`不安全,可能导致缓冲区溢出。
最后,需要将`mytel[cnt] = buff;`改为`mytel[cnt] = *p;`,因为`buff`的值在每次输入后都会被覆盖,但是`mytel`数组中存储的是每次输入的值,不能被覆盖。
修改后的代码如下:
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)