순차 액세스 파일에 새 레코드를 추가하는 데 문제가 있습니다. 사용자가 모델 코드를 입력하면 프로그램은이를 확인하고 중복이 발견 된 경우 동일한 이름의 항목이 이미 있음을 표시합니다. 내 코드를 실행하면, 그냥 보여줍니다 때c 프로그램. 텍스트 파일에 레코드 추가시 문제가 발생합니다.
Record already exists
Record already exists
Record already exists
Record already exists
가 : 1. 코드가 파일에 존재 2. 코드가 파일에 존재하지 않습니다.
누군가 나를 도와 줄 수 있습니까? 어떤 도움을 주시면 감사하겠습니다.
//function to add record
void stockEntry(){
FILE *fp;
fp = fopen("stock.txt","a+");
//fopen opens file.Exit program if unable to create file
if(fp==NULL){
puts("File could not be opened");
}//ends if
//obtains information from user
else{
printf("\nEnter model code,model name,cost,price and quantity(Use space to separate inputs)\n");
scanf("%s %s %f %f %d",code,a.name,&a.cost,&a.price,&a.quantity);
rewind (fp);
do{
if(strcmp(code,a.code)!=0){
//write details into stock.txt
fprintf(fp,"%s %s %.2f %.2f %d\n",a.code,a.name,a.cost,a.price,a.quantity);
}
else{
printf("Record already exists\n");
}
}while(fscanf(fp,"%s %s %f %f %d\n",a.code,a.name,&a.cost,&a.price,&a.quantity)==5);
}
//fclose closes file
fclose(fp);
}
어디'code'하고있는'A' 초기화/선언과 그 유형은 무엇입니까? – simonc
코드는 본체 전에 선언됩니다 – user3676752
은 구조체 배열의 이름이며 본체 전에 초기화되었습니다 – user3676752