c语言基于文件操作的植物信息管理系统
时间: 2023-07-26 08:36:06 浏览: 55
好的,我可以帮你解答关于基于文件操作的植物信息管理系统的问题。
首先,基于文件操作的植物信息管理系统可以实现对植物信息的增加、修改、删除、查询等功能。下面是一个简单的示例程序:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Plant{
char name[50];
char type[50];
char origin[50];
float price;
};
void addPlant(){
FILE *fp;
struct Plant p;
fp = fopen("plants.dat", "ab");
printf("\nEnter the name, type, origin and price of the plant:\n");
scanf("%s%s%s%f", p.name, p.type, p.origin, &p.price);
fwrite(&p, sizeof(p), 1, fp);
fclose(fp);
printf("\nPlant added successfully!\n");
}
void displayPlant(struct Plant p){
printf("\nName: %s\nType: %s\nOrigin: %s\nPrice: %.2f\n", p.name, p.type, p.origin, p.price);
}
void searchPlant(){
FILE *fp;
struct Plant p;
char name[50];
int found = 0;
fp = fopen("plants.dat", "rb");
printf("\nEnter the name of the plant to search:\n");
scanf("%s", name);
while(fread(&p, sizeof(p), 1, fp) == 1){
if(strcmp(p.name, name) == 0){
displayPlant(p);
found = 1;
break;
}
}
fclose(fp);
if(found == 0){
printf("\nPlant not found!\n");
}
}
void updatePlant(){
FILE *fp;
struct Plant p;
char name[50];
int found = 0;
fp = fopen("plants.dat", "rb+");
printf("\nEnter the name of the plant to update:\n");
scanf("%s", name);
while(fread(&p, sizeof(p), 1, fp) == 1){
if(strcmp(p.name, name) == 0){
printf("\nEnter the new name, type, origin and price of the plant:\n");
scanf("%s%s%s%f", p.name, p.type, p.origin, &p.price);
fseek(fp, -sizeof(p), SEEK_CUR);
fwrite(&p, sizeof(p), 1, fp);
found = 1;
break;
}
}
fclose(fp);
if(found == 0){
printf("\nPlant not found!\n");
}
else{
printf("\nPlant updated successfully!\n");
}
}
void deletePlant(){
FILE *fp, *temp;
struct Plant p;
char name[50];
int found = 0;
fp = fopen("plants.dat", "rb");
temp = fopen("temp.dat", "wb");
printf("\nEnter the name of the plant to delete:\n");
scanf("%s", name);
while(fread(&p, sizeof(p), 1, fp) == 1){
if(strcmp(p.name, name) != 0){
fwrite(&p, sizeof(p), 1, temp);
}
else{
found = 1;
}
}
fclose(fp);
fclose(temp);
remove("plants.dat");
rename("temp.dat", "plants.dat");
if(found == 0){
printf("\nPlant not found!\n");
}
else{
printf("\nPlant deleted successfully!\n");
}
}
void displayAllPlants(){
FILE *fp;
struct Plant p;
fp = fopen("plants.dat", "rb");
while(fread(&p, sizeof(p), 1, fp) == 1){
displayPlant(p);
}
fclose(fp);
}
int main(){
int choice;
while(1){
printf("\n----- Plant Information Management System -----\n");
printf("\n1. Add Plant\n2. Search Plant\n3. Update Plant\n4. Delete Plant\n5. Display All Plants\n6. Exit\n");
printf("\nEnter your choice:\n");
scanf("%d", &choice);
switch(choice){
case 1:
addPlant();
break;
case 2:
searchPlant();
break;
case 3:
updatePlant();
break;
case 4:
deletePlant();
break;
case 5:
displayAllPlants();
break;
case 6:
exit(0);
default:
printf("\nInvalid choice!\n");
}
}
return 0;
}
```
在这个程序中,我们定义了一个植物结构体,包含植物的名称、类型、产地和价格。我们使用文件操作对植物信息进行管理,包括增加、查询、修改、删除和显示所有植物信息。我们使用二进制文件读写操作,将植物信息保存在名为“plants.dat”的文件中。
程序中的函数说明如下:
- addPlant():添加植物信息。
- displayPlant():显示植物信息。
- searchPlant():查询植物信息。
- updatePlant():更新植物信息。
- deletePlant():删除植物信息。
- displayAllPlants():显示所有植物信息。
在主函数中,我们使用一个无限循环来实现不断读取用户的选择,并调用相应的函数来处理。
这是一个简单的基于文件操作的植物信息管理系统的示例程序,你可以根据自己的需求对其进行改进和扩展。