我在这里有一些代码而且我正在努力解决它,因为我似乎无法掌握这个文件指针的东西。我很擅长处理文件。我已经看到了这样的其他问题,我尝试了适用于其他人的解决方案,但由于某种原因,他们不适合我。以下是问题所在代码的一部分:
void writeRentals(char *filename, RentalT *allRentals, int totalRentals);
int readRentals(char *filename, RentalT *allRentals);
void writeCars(char *filename, CarT *allCars, int totalCars);
int readCars(char *filename, CarT *allCars);
int main() {
CarT allCars[255]; // a list of cars
RentalT allRentals[255]; // a list of rentals
int totalCars = 0; // number of all cars owned
int totalRentals = 0; // number of rented cars
int menu_choice, renterIndex, carId;
char renterName[20], carMake[20];
// read data from the files
char rentalFilename[255];
char carFilename[255];
printf("Please enter the name of the rental records file:");
fscanf("%s", &rentalFilename);
printf("\nPlease enter the name of the car records file:");
fscanf("%s", &carFilename);
totalCars = readCars(carFilename, allCars);
totalRentals = readRentals(&rentalFilename, allRentals);
这是我编译时遇到的错误:
----jGRASP exec: gcc -g -o carRental.exe carRental.c
carRental.c: In function 'main':
carRental.c:57:9: warning: passing argument 1 of 'fscanf' from incompatible pointer type [-Wincompatible-pointer-types]
fscanf("%s", &rentalFilename);
^
In file included from carRental.c:1:0:
stdio.h:385:15: note: expected 'FILE * restrict {aka struct _iobuf * restrict}' but argument is of type 'char *'
int __cdecl fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
^
carRental.c:57:15: warning: passing argument 2 of 'fscanf' from incompatible pointer type [-Wincompatible-pointer-types]
fscanf("%s", &rentalFilename);
^
In file included from carRental.c:1:0:
stdio.h:385:15: note: expected 'const char * restrict' but argument is of type 'char (*)[255]'
int __cdecl fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
^
carRental.c:59:9: warning: passing argument 1 of 'fscanf' from incompatible pointer type [-Wincompatible-pointer-types]
fscanf("%s", &carFilename);
^
In file included from carRental.c:1:0:
stdio.h:385:15: note: expected 'FILE * restrict {aka struct _iobuf * restrict}' but argument is of type 'char *'
int __cdecl fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
^
carRental.c:59:15: warning: passing argument 2 of 'fscanf' from incompatible pointer type [-Wincompatible-pointer-types]
fscanf("%s", &carFilename);
^
In file included from carRental.c:1:0:
stdio.h:385:15: note: expected 'const char * restrict' but argument is of type 'char (*)[255]'
int __cdecl fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
^
carRental.c:62:29: warning: passing argument 1 of 'readRentals' from incompatible pointer type [-Wincompatible-pointer-types]
totalRentals = readRentals(&rentalFilename, allRentals);
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
您已经搞砸了scanf
和fscanf
的使用情况。你错过了使用fscanf时传递的第一个参数。
首先打开一个文件,然后将相应的文件指针作为第一个参数传递给fscanf。
使用scanf
从控制台扫描字符串,如下所示。
printf("Please enter the name of the rental records file:");
scanf("%s", rentalFilename);
//No need of &rentalFile
然后使用文件指针打开此文件。
File *fp = fopen (rentalFilename, "r");//r for reading
然后您可以使用fscanf
从文件中读取
fscanf(fp,"%s",line);//Note line is a char array
//This was just an example
您可能希望查看fscanf
here的示例。
正如chux在评论中所建议的那样,探索在C中扫描字符串的各种方法.scanf的替代方法是gets(),fgets()。看看哪个适合您的目的。避免使用弃用的方法。