struct info
{
float cond_cost, vat,insure_total, insure_cost;
char insure_name[10],insure_cond[10], registration[10], date[10],surname[20],name[20],car_num[10];
int num;
};
displayinfo()//this function searches for the record using the registration number
{
char carnum[10];
int foundit;
struct info i={0.0,0.0,0.0,0.0,"","","","","","","",0};
system("cls");
foundit = 0;
FILE *insure;
if((insure= fopen("insurance.txt","r"))==NULL)
{
printf("File cannot be opened");
system("pause");
}
else
{
FILE *reg;
if((reg= fopen("search.txt","a"))==NULL)
{
printf("File cannot be opened");
system("pause");
}
else
{
printf("Enter car registration number: ");
scanf("%s",carnum);
while(!feof(reg))
{
fread(&i,1,sizeof(struct info),insure);
if ((strcmp(carnum,i.registration)==0))
{
//this section will print the searched record via registration number
printf("|---------------------------------|\n");
printf("| INSURANCE |\n");
printf("|---------------------------------|\n");
printf("| Car Registration number :%s\n", i.registration);
printf("| Insurance holder first name:%s\n", i.name);
printf("| Insurance holder last name :%s\n", i.surname);
printf("| Insurance car covered :%s\n", &i.insure_name);
printf("| Insurance car coverage cost:$%7.2f\n", i.insure_cost);
printf("| Car class :%s\n", i.insure_cond);
printf("| Car class cost :$%7.2f\n", i.cond_cost);
printf("| Vat :$%7.2f\n", i.vat);
printf("| Total insurance cost :$%7.2f\n", i.insure_total);
printf("| Date :%s\n", i.date);
printf("|---------------------------------|\n");
foundit=1;
system("pause");
break;
}//endif
if (foundit !=1)
{
printf("Record cannot be found\n");
system("pause");
return 0;
}
}//ends while loop
fclose(reg);
fclose(insure);
}
}
}
注册号用于查找该人的姓名,汽车,汽车费用,汽车等等的记录。但除了姓氏变量外,所有变量都会打印出来。
答案 0 :(得分:0)
此示例从文件读取和写入结构,并可以执行匹配的字符串。您看到打印姓氏没有问题。
createSymbolicLinkAtURL(_:withDestinationURL:)
<强>测试强>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info {
float cond_cost, vat, insure_total, insure_cost;
char insure_name[10], insure_cond[10], registration[10], date[10], surname[20], name[20], car_num[10];
int num;
};
char carnum[10];
struct info i = {0.0, 0.0, 0.0, 0.0, "", "", "", "", "", "", "", 0};
FILE *fp;
int main() {
struct info insurances[2];
fp = fopen("output.dat", "ab");
for (int i = 0; i < 2; i++) {
printf("Enter name:");
scanf("%s", insurances[i].name);
printf("Enter surname:");
scanf("%s", insurances[i].surname);
printf("Enter registration number");
scanf("%s", insurances[i].registration);
}
fwrite(insurances, sizeof(struct info), 2, fp);
fclose(fp);
fp = fopen("output.dat", "rb");
fread(insurances, sizeof(struct info), 2, fp);
printf("Enter car registration number: ");
scanf("%s", carnum);
for (int i = 0; i < 2; i++) {
if ((strcmp(carnum, insurances[i].registration) == 0)) {
printf("|---------------------------------|\n");
printf("| INSURANCE |\n");
printf("|---------------------------------|\n");
printf("| Car Registration number :%s\n", insurances[i].registration);
printf("| Insurance holder first name:%s\n", insurances[i].name);
printf("| Insurance holder last name :%s\n", insurances[i].surname);
printf("| Insurance car covered :%s\n", insurances[i].insure_name);
printf("| Insurance car coverage cost:$%7.2f\n", insurances[i].insure_cost);
printf("| Car class :%s\n", insurances[i].insure_cond);
printf("| Car class cost :$%7.2f\n", insurances[i].cond_cost);
printf("| Vat :$%7.2f\n", insurances[i].vat);
printf("| Total insurance cost :$%7.2f\n", insurances[i].insure_total);
printf("| Date :%s\n", insurances[i].date);
printf("|---------------------------------|\n");
}
}
}
FILE *fp;
int main2()//this function searches for the record using the registration number
{
char carnum[10];
int foundit;
struct info i = {0.0, 0.0, 0.0, 0.0, "Superman", "A", "ABC123", "Sunday", "Kent", "Clark", "SuperCar", 0};
fp = fopen("output.dat", "ab");
fwrite(&i, sizeof(struct info), 1, fp);
fclose(fp);
system("cls");
foundit = 0;
FILE *insure;
if ((insure = fopen("output.dat", "r")) == NULL) {
printf("File cannot be opened");
system("pause");
}
else {
FILE *reg;
if ((reg = fopen("search.txt", "a")) == NULL) {
printf("File cannot be opened");
system("pause");
}
else {
printf("Enter car registration number: ");
scanf("%s", carnum);
while (!feof(reg)) {
fread(&i, 1, sizeof(struct info), insure);
printf("Name %s\n", i.name);
//printf("%d", data[i].age)
if ((strcmp(carnum, i.registration) == 0)) {
// fread(data, sizeof(struct chris), 10, fp);
;
//this section will print the searched record via registration number
printf("|---------------------------------|\n");
printf("| INSURANCE |\n");
printf("|---------------------------------|\n");
printf("| Car Registration number :%s\n", i.registration);
printf("| Insurance holder first name:%s\n", i.name);
printf("| Insurance holder last name :%s\n", i.surname);
printf("| Insurance car covered :%s\n", i.insure_name);
printf("| Insurance car coverage cost:$%7.2f\n", i.insure_cost);
printf("| Car class :%s\n", i.insure_cond);
printf("| Car class cost :$%7.2f\n", i.cond_cost);
printf("| Vat :$%7.2f\n", i.vat);
printf("| Total insurance cost :$%7.2f\n", i.insure_total);
printf("| Date :%s\n", i.date);
printf("|---------------------------------|\n");
foundit = 1;
system("pause");
break;
}//endif
if (foundit != 1) {
printf("Record cannot be found\n");
system("pause");
return 0;
}
}//ends while loop
fclose(reg);
fclose(insure);
}
}
}