【发布时间】:2015-10-15 20:28:43
【问题描述】:
我对这段代码有一些问题,我想编译它,但它没有编译,因为有问题。我告诉了那里的错误代码......
#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);在将标题读入它之前擦洗随机数据字段 - 在调试时有用记录。 -
将格式字符串中的
%d更改为%u,以匹配无符号数。
标签: c