我對這段代碼有一些問題,我想編譯它,但它不是編譯的,錯誤。我告訴錯誤的代碼那裏......C格式'%d'需要類型爲'int *'的參數,但參數2的類型爲'unsigned int'[-Wformat =]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
typedef struct{
int film_id;
char title[255];
char description[1023];
unsigned int release_year;
char rental_duration;
float rental_rate;
unsigned char length;
float replacement_cost;
char rating[10];
char last_update[30];
} RECORD_t, *RECORD;
int main(){
int i,R1;
char R[1];
RECORD rec = (RECORD)malloc(sizeof(RECORD_t)*MAX);
FILE *file = fopen("data.txt", "rb");
if (file == NULL) {
printf("Cannot open the file.\n");
exit(0);
}
fread(rec, sizeof(RECORD_t)*MAX, 1, file);
fclose(file);
printf("İd Numarası Giriniz : \n");
scanf("%d",&R1);
for (i=0; i<1000; i++){
if (R1 == rec[i].film_id) {
printf("TITLE: %s\n", rec[i].title); printf("Enter New TITLE : "); scanf("%s",rec[i].title);
printf("DESCRIPTION: %s\n", rec[i].description); printf("Enter New Description : "); scanf("%s",rec[i].description);
// My problem is this line :-------------------
printf("RELEASE YEAR: %d\n", rec[i].release_year); printf("Enter New RELEASE YEAR : "); scanf("%d",rec[i].release_year);
printf("RENTAL DURATION: %d\n", rec[i].rental_duration); printf("Enter New RENTAL DURATION : "); scanf("%d",rec[i].rental_duration);
printf("RENTAL RATE: %f\n", rec[i].rental_rate); printf("Enter New RENTAL RATE : "); scanf("%f",rec[i].rental_rate);
printf("REPLACEMENT COST: %f\n", rec[i].replacement_cost); printf("Enter New REPLACEMENT COST : "); scanf("%f",rec[i].replacement_cost);
printf("RATING: %s\n", rec[i].rating); printf("Enter New RATING : "); scanf("%s",rec[i].rating);
printf("LAST UPDATE: %s\n", rec[i].last_update); printf("Enter New LAST UPDATE : "); scanf("%s",rec[i].last_update);
}
}
}
file = fopen("data.txt", "wb");
fwrite(rec, sizeof(RECORD_t)*MAX, 1, file);
fclose(file);
free(rec);
return 1;
}
只有int和float變量不工作,而我是編譯
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned int’ [-Wformat=]
printf("RELEASE YEAR: %d\n", rec[i].release_year); printf("Enter New RELEASE YEAR : "); scanf("%d",rec[i].release_year);
^
請大家幫幫我://
1)'的scanf( 「%s」 時,REC [I] .title僞);'不能防止過分解讀'title'。最好使用'scanf(「%254s」,rec [i] .title);'2)'scanf(「%s」,rec [i] .title);'不能用空格3 )推薦'memset(rec [i] .title,0,sizeof rec [i] .title);'在讀入標題之前清理隨機數據的字段 - 在調試時沒有垃圾記錄時很有用。 – chux
將格式字符串中的'%d'更改爲'%u',以匹配無符號數字。 –