gpt4 book ai didi

c - Displayinfo() 函数不起作用,它表示文件未创建。该函数的目的是读取特定行

转载 作者:行者123 更新时间:2023-11-30 16:06:45 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 500

typedef struct
{
char Matricule[50];
char Model[30];
char Price[30];
int KilometrePerHour;
char Etat[50];
}
Voiture;

Voiture info[Max];
void stockinfo();
void Saveinfo();
void Displayinfo();
void displayAll();
int n;

void stockinfo()
{
int i;
printf("How many Cars You Want To Add ? \n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter The Matricule : \n");
scanf("%s", info[i].Matricule);
printf("Enter The Module : \n");
scanf("%s", info[i].Model);
printf("Enter The Price : \n");
scanf("%s", info[i].Price);
printf("Enter The Kilometere Per Hour : \n");
scanf("%d", &info[i].KilometrePerHour);
printf("Enter The Case : \n");
scanf("%s", info[i].Etat);
}
}

void Saveinfo()
{
FILE * save;
save = fopen("CarsParc.doc", "a");
if (save == NULL)
{
printf("The file is Not Created Succefully..\n");
}
else
{
int i;
for (i = 0; i < n; i++)
{
fprintf(save, "\t\tThe Information Of %s Car..\n", info[i].Model);
fprintf(save, "The Matricule :%s\n", info[i].Matricule);
fprintf(save, "The Model :%s\n", info[i].Model);
fprintf(save, "The Price :%s\n", info[i].Price);
fprintf(save, "The Kilometre/h :%d\n", info[i].KilometrePerHour);
fprintf(save, "Tha Case (New/Used) :%s\n", info[i].Etat);
fprintf(save, "\n\n");
}
}
fclose(save);
}

/*i meant this function*/
void Displayinfo()
{
FILE * save;
save = fopen("CarsParc.doc", "r");
if (save == NULL)
{
printf("The file is Not Created Succefully..\n");
}
else
{
char T[100];
char mtrcl[100];
do { fgets(T, 100, save);
printf("Enter The Matricule : \n");
scanf("%s", mtrcl);
int i;
if (strcmp(T, mtrcl) == 0)
{
for (i = 1; i <= 7; i++)
{
fgets(T, 100, save);
printf("%s", T);
}
}
} while (strcmp(T, mtrcl) != 0);
fclose(save);
}
}

void displayAll()
{
FILE * save;
save = fopen("CarsParc.doc", "r");
if (save == NULL)
{
printf("The file is Not Created Succefully..\n");
}
else
{
char copy;
do { copy = fgetc(save);
printf("%c", copy);
} while (copy != EOF);
fclose(save);
}
}

main()
{
/*code */
int choice;
printf("\t\t Welcome To Cars Parc : \n");
printf("1-Add Car : |Click One.. | \n");
printf("2-Search for Specific Car : |Click Two.. |\n");
printf("3-See All The informations : |Click Three..|\n");
scanf("%d", &choice);
system("cls");
do {
if (choice == 1)
{
stockinfo();
Saveinfo();
system("cls");
int back;
printf("The Informations Of This %s Car in Saved Now..\n", info[Max].Model);
printf("To Back To The Menue Click 0 ..\n");
scanf("%d", &back);
if (back == 0)
{
system("cls");
main();
}
else
{
printf("This is The End Of The Programme..");
}
}
if (choice == 2)
{
Displayinfo();
int back;
printf("\n\n\n");
printf("To Back To The Menue Click 0 ..\n");
scanf("%d", &back);
if (back == 0)
{
system("cls");
main();
}
else
{
printf("This is The End Of The Programme..");
}
}
if (choice == 3)
{
displayAll();
int back;
printf("\n\n\n");
printf("To Back To The Menue Click 0 ..\n");
scanf("%d", &back);
if (back == 0)
{
system("cls");
main();
}
else
{
printf("This is The End Of The Programme..");
}
}
} while (choice != 3);
}

最佳答案

您正在打开文件进行读取(“r”)。如果不存在,则不会创建它。您需要使用适当的模式(w 或 a)。

来自手册页:

``r''   Open for reading.  The stream is positioned at the beginning of
the file. Fail if the file does not exist.

``w'' Open for writing. The stream is positioned at the beginning of
the file. Create the file if it does not exist.

``a'' Open for writing. The stream is positioned at the end of the
file. Subsequent writes to the file will always end up at the
then current end of file, irrespective of any intervening
fseek(3) or similar. Create the file if it does not exist.

关于c - Displayinfo() 函数不起作用,它表示文件未创建。该函数的目的是读取特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59869580/

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