c语言实现地点查询次数记录,并将其记录到txt文档中
时间: 2023-09-19 11:10:49 浏览: 42
以下是一个简单的C语言程序,可以实现查询次数的记录,并将其记录到txt文档中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LOCATION_LENGTH 50 // 地点名称最大长度
/* 定义地点结构体 */
typedef struct Location {
char name[MAX_LOCATION_LENGTH]; // 地点名称
int count; // 查询次数
} Location;
/* 定义函数:从txt文件中读取地点列表 */
int read_locations_from_file(char* filename, Location** locations, int* num_locations) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return -1;
}
int max_locations = 10;
*locations = (Location*)malloc(max_locations * sizeof(Location));
*num_locations = 0;
char line[MAX_LOCATION_LENGTH + 10];
while (fgets(line, MAX_LOCATION_LENGTH + 10, fp) != NULL) {
if (*num_locations >= max_locations) {
max_locations *= 2;
*locations = (Location*)realloc(*locations, max_locations * sizeof(Location));
}
sscanf(line, "%s %d", (*locations)[*num_locations].name, &((*locations)[*num_locations].count));
(*num_locations)++;
}
fclose(fp);
return 0;
}
/* 定义函数:向txt文件中写入地点列表 */
int write_locations_to_file(char* filename, Location* locations, int num_locations) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return -1;
}
for (int i = 0; i < num_locations; i++) {
fprintf(fp, "%s %d\n", locations[i].name, locations[i].count);
}
fclose(fp);
return 0;
}
/* 定义函数:查询某个地点 */
void query_location(Location* locations, int num_locations, char* name) {
for (int i = 0; i < num_locations; i++) {
if (strcmp(locations[i].name, name) == 0) {
locations[i].count++;
printf("%s has been queried %d times\n", name, locations[i].count);
return;
}
}
printf("%s has not been queried before\n", name);
}
int main() {
Location* locations;
int num_locations;
/* 从txt文件中读取地点列表 */
if (read_locations_from_file("locations.txt", &locations, &num_locations) != 0) {
return -1;
}
/* 进行地点查询 */
char name[MAX_LOCATION_LENGTH];
while (1) {
printf("Please input a location name (q to quit):\n");
scanf("%s", name);
if (strcmp(name, "q") == 0) {
break;
}
query_location(locations, num_locations, name);
}
/* 将地点列表写入txt文件 */
if (write_locations_to_file("locations.txt", locations, num_locations) != 0) {
return -1;
}
free(locations);
return 0;
}
```
这个程序中,我们定义了一个地点结构体,包含地点名称和查询次数两个属性。我们首先从txt文件中读取地点列表,然后进行地点查询,如果查询到了某个地点,则将其查询次数加1。最后,我们将更新后的地点列表写入txt文件中。
你可以在程序中修改文件名和文件路径,以适应你的需要。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)