#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct order{
char name[1000];
int price;
struct list *next;
} LIST;
void del_name_write(LIST *node,char *filename,char *del){
FILE *fp;
LIST D[8]={{"sugar",100},{"salt",100},{"meat",300},{"milk",110},{"candy",150},{"gum",90},{"fish",300},{"wine",2000}};
int k = 0,j;
if(node == NULL){
return;
}
if((fp = fopen(filename, "w")) == NULL){
fprintf(stderr, "Could not open file\n");
exit(1);
}
while(1){
for(j=0;j<8;j++){
if(strcmp(D[j].name,&node->name[0])==0){
node->price = D[j].price;
}
}
fprintf(fp, "名:%s 代金:%d\n", node->name, node->price);
node = node->next;
if(node->next == NULL){
break;
}
}
fclose(fp);
}
LIST *file_read(char *filename){
FILE *fp;
LIST data;
LIST *start = NULL,*tmp;
int i=0;
if((fp=fopen(filename,"r")) == NULL){
fprintf(stderr, "Could not open file\n");
exit(1);
}
while(1){
if(feof(fp) != 0){
break;
}
if(fscanf( fp, "%s", &data)==0){
break;
}
if(start == NULL){
start=(LIST *)malloc(sizeof(LIST));
tmp=start;
}else{
tmp->next=(LIST *)malloc(sizeof(LIST));
tmp=tmp->next;
}
memcpy(tmp,&data,sizeof(LIST));
tmp->next=NULL;
}
fclose(fp);
return start;
}
int main(){
int k=0;
char *p=(char *)malloc(10*sizeof(char));
LIST *file_list;
printf("削除したい商品名を入力する:");
scanf("%s",p);
printf("\n");
file_list = file_read("test_1.txt");
del_name_write(file_list,"test_2.txt",p);
return 0;
}