c语言实现从txt文档中查找地点查询记录,并将新的查询添加到txt文档的次数记录中
时间: 2023-11-28 09:47:28 浏览: 29
可以按照以下步骤实现:
1. 打开txt文档,读取其中的地点查询记录,并存储到一个数据结构中,如数组或链表。
2. 提示用户输入要查询的地点,然后在记录中查找是否存在该地点。
3. 如果存在该地点,则将该记录的次数加1,并将更新后的记录重新写回txt文档中。
4. 如果不存在该地点,则将新的查询添加到记录中,并将更新后的记录重新写回txt文档中。
下面是一份简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000
#define MAX_LEN 100
struct Record {
char place[MAX_LEN];
int count;
};
int read_records(struct Record records[]);
void write_records(struct Record records[], int count);
int find_record(struct Record records[], int count, char *place);
void add_record(struct Record records[], int *count, char *place);
int main() {
struct Record records[MAX_RECORDS];
int count = read_records(records);
char place[MAX_LEN];
printf("Enter a place to search: ");
fgets(place, MAX_LEN, stdin);
place[strcspn(place, "\n")] = '\0';
int index = find_record(records, count, place);
if (index != -1) {
records[index].count++;
} else {
add_record(records, &count, place);
}
write_records(records, count);
return 0;
}
int read_records(struct Record records[]) {
FILE *file = fopen("records.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int count = 0;
char line[MAX_LEN];
while (fgets(line, MAX_LEN, file) != NULL) {
char *token = strtok(line, " ");
strcpy(records[count].place, token);
records[count].count = atoi(strtok(NULL, " "));
count++;
}
fclose(file);
return count;
}
void write_records(struct Record records[], int count) {
FILE *file = fopen("records.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
for (int i = 0; i < count; i++) {
fprintf(file, "%s %d\n", records[i].place, records[i].count);
}
fclose(file);
}
int find_record(struct Record records[], int count, char *place) {
for (int i = 0; i < count; i++) {
if (strcmp(records[i].place, place) == 0) {
return i;
}
}
return -1;
}
void add_record(struct Record records[], int *count, char *place) {
strcpy(records[*count].place, place);
records[*count].count = 1;
(*count)++;
}
```
在上面的代码中,我们定义了一个`Record`结构体表示地点查询记录,其中包含两个成员变量:`place`表示地点名称,`count`表示查询次数。我们还定义了一些常量和函数来读取和写入txt文档中的记录,以及查找和添加地点查询记录。在`main()`函数中,我们首先读取txt文档中的记录,然后提示用户输入要查询的地点。接着,我们在记录中查找该地点,如果存在,则将该记录的次数加1;否则,将新的查询添加到记录中。最后,我们将更新后的记录写回txt文档中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)