gpt4 book ai didi

c - 遇到 fscanf 文件指针错误问题

转载 作者:行者123 更新时间:2023-11-30 19:34:36 27 4
gpt4 key购买 nike

我这里有一些代码,但我正在努力解决它,因为我似乎无法掌握这个文件指针的东西。我对使用文件还很陌生。我见过类似的其他问题,并且尝试了对其他人有效的解决方案,但由于某种原因它们对我不起作用。这是出现问题的代码部分:

    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);

非常感谢任何帮助,谢谢!

最佳答案

您搞乱了 scanffscanf 的使用。使用 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()。看看哪个最适合您的目的。避免使用已弃用的方法。

关于c - 遇到 fscanf 文件指针错误问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43685802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com