c语言怎么把信息录入文件
时间: 2023-09-27 13:03:54 浏览: 40
要将信息录入文件,可以使用标准库函数fopen和fprintf。首先使用fopen函数打开一个文件,并获得一个文件指针,然后使用fprintf函数将信息写入文件中。最后使用fclose函数关闭文件。下面是一个简单的示例代码:
```
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "这是一条测试信息\n");
fclose(fp);
return 0;
}
```
在上面的代码中,我们使用fopen函数打开了一个名为"data.txt"的文件,并且以写入模式打开。然后使用fprintf函数将一条测试信息写入文件中。最后使用fclose函数关闭了文件。
相关问题
C语言将表格信息录入文件并查看
要将表格信息录入文件并查看,可以使用文件操作函数和循环结构来实现。以下是一个简单的示例代码:
```c
#include <stdio.h>
int main() {
// 定义一个二维数组存储表格信息
int table[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// 获取表格的行数和列数
int rows = sizeof(table) / sizeof(table[0]);
int cols = sizeof(table[0]) / sizeof(table[0][0]);
// 将表格信息写入文件
FILE *fp = fopen("table.txt", "w");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fprintf(fp, "%d\t", table[i][j]);
}
fprintf(fp, "\n");
}
fclose(fp);
// 从文件中读取表格信息并输出
fp = fopen("table.txt", "r");
printf("表格信息如下:\n");
int num;
while (fscanf(fp, "%d", &num) != EOF) {
printf("%d\t", num);
if (ftell(fp) % (cols * sizeof(int)) == 0) {
printf("\n");
}
}
fclose(fp);
return 0;
}
```
示例代码中,我们使用双重循环将表格信息写入文件,然后再从文件中读取表格信息并输出。其中 `fopen()` 函数用来打开文件,`fprintf()` 函数用来写入文件,`fclose()` 函数用来关闭文件,`fscanf()` 函数用来从文件中读取数据,`ftell()` 函数用来获取文件指针的当前位置。最后输出的结果与前面的示例代码相同。
C语言将航班信息录入系统
要将航班信息录入系统,可以使用结构体和文件操作函数来实现。以下是一个简单的示例代码:
```c
#include <stdio.h>
#define MAX_FLIGHTS 100
typedef struct flight {
char number[10]; // 航班号
char departure[20]; // 起飞地
char destination[20]; // 目的地
char date[20]; // 日期
} Flight;
int main() {
// 定义一个数组存储航班信息
Flight flights[MAX_FLIGHTS];
// 定义一个计数器记录航班数量
int count = 0;
// 录入航班信息
printf("请输入航班信息,输入 q 结束录入:\n");
while (count < MAX_FLIGHTS) {
printf("请输入第%d个航班信息:\n", count+1);
printf("航班号:");
scanf("%s", flights[count].number);
if (flights[count].number[0] == 'q') {
break;
}
printf("起飞地:");
scanf("%s", flights[count].departure);
printf("目的地:");
scanf("%s", flights[count].destination);
printf("日期:");
scanf("%s", flights[count].date);
count++;
}
// 将航班信息写入文件
FILE *fp = fopen("flights.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %s %s %s\n", flights[i].number, flights[i].departure, flights[i].destination, flights[i].date);
}
fclose(fp);
// 从文件中读取航班信息并输出
fp = fopen("flights.txt", "r");
printf("航班信息如下:\n");
Flight f;
while (fscanf(fp, "%s %s %s %s", f.number, f.departure, f.destination, f.date) != EOF) {
printf("航班号:%s,起飞地:%s,目的地:%s,日期:%s\n", f.number, f.departure, f.destination, f.date);
}
fclose(fp);
return 0;
}
```
示例代码中,我们定义了一个 `Flight` 结构体表示航班信息,使用循环录入航班信息,并将其写入文件。然后从文件中读取航班信息并输出。其中 `fopen()` 函数用来打开文件,`fprintf()` 函数用来写入文件,`fclose()` 函数用来关闭文件,`fscanf()` 函数用来从文件中读取数据。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)