从文件覆盖C中struct中的索引
我已经实现了这样的代码;如果队列中的车辆想要更改选择的清洗类型,则必须能够更改该车辆的清洗类型: 该程序从一个名为CarsQ.txt的文件中读取数据;包含 现在的问题是,为什么它不覆盖wash类型,它只是打印队列从文件覆盖C中struct中的索引,c,string,struct,linked-list,overwrite,C,String,Struct,Linked List,Overwrite,我已经实现了这样的代码;如果队列中的车辆想要更改选择的清洗类型,则必须能够更改该车辆的清洗类型: 该程序从一个名为CarsQ.txt的文件中读取数据;包含 现在的问题是,为什么它不覆盖wash类型,它只是打印队列 AV96888 VW alm KD65656 Audi luksus AX21878 Ford alm CN32323 VW alm NB21214 Ford luksus UM21878 Ford alm AV54361 Tesla luksus ^不同的文件,它有两种清洗类型,a
AV96888 VW alm
KD65656 Audi luksus
AX21878 Ford alm
CN32323 VW alm
NB21214 Ford luksus
UM21878 Ford alm
AV54361 Tesla luksus
^不同的文件,它有两种清洗类型,alm和luksus(普通和豪华)
#包括
#包括
#包括
#包括
#定义N 100
结构车
{
字符编号板[N];
煤焦品牌[N];
炭洗;
结构车*下一步;
结构车*prev;
}汽车;
typedef结构队列
{
结构车*a[N];
整数合计;
汽车前部;
汽车后部;
}排队;
void New_Wash_类型(结构队列*k,常量字符*Tree)
{
结构车*p;
p=malloc(sizeof(struct Car));
strcpy(p->Wash,Tree);
p=k->a[1];
}
int main()
{
printf(“\n打印队列:”);
结构车*p;
结构队列k;
字符一[N],二[N],树[N];
文件*fp;
fp=fopen(“CarsQ.txt”,“r”);
而(fscanf(fp,“%s%s%s”,一,二,树)==3)
{
p=malloc(sizeof(struct Car));
strcpy(p->Numberplate,一);
strcpy(p->Brand,两个);
strcpy(p->Wash,Tree);
printf(“\n%s%s%s”,p->Numberplate,p->Brand,p->Wash);
k、 a[k.Total++]=p;
}
fclose(fp);
printf(“\n\n第12条:”);
新型洗衣机(&k,“豪华型”);
返回0;
}
您从未初始化过k.Total=0代码>New\u Wash\u Type
不会覆盖任何内容。它创建了一个新的汽车,将新的清洗放入其中,但不会用它更新队列。new\u wash\u Type
应该覆盖k->a[1]->wash
,它不需要分配一个新的Carit,这只是更大代码的一小部分,其中k.total=0,已初始化,谢谢。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define N 100
typedef struct Car
{
char Numberplate[N];
char Brand[N];
char Wash[N];
struct car *next;
struct car *prev;
} Car;
typedef struct queue
{
struct Car *a[N];
int Total;
Car *front;
Car *rear;
} queue;
void New_Wash_Type(struct queue *k,const char *Tree)
{
struct Car *p;
p=malloc(sizeof(struct Car));
strcpy(p->Wash,Tree);
p = k->a[1];
}
int main ()
{
printf("\nPrint queue:");
struct Car *p;
struct queue k;
char One[N], Two[N], Tree[N];
FILE * fp;
fp = fopen ("CarsQ.txt", "r");
while(fscanf(fp,"%s %s %s",One,Two,Tree)==3)
{
p=malloc(sizeof(struct Car));
strcpy(p->Numberplate,One);
strcpy(p->Brand,Two);
strcpy(p->Wash,Tree);
printf("\n%s %s %s",p->Numberplate,p->Brand,p->Wash);
k.a[k.Total++] =p;
}
fclose(fp);
printf("\n\nOpgave 12:");
New_Wash_Type(&k,"luxury");
return 0;
}