Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Displayinfo()函数不起作用,它表示文件未创建。此功能的目的是读取特定行 #包括 #包括 #包括 #定义最大值500 类型定义结构 { 字符矩阵[50]; char模型[30]; 煤焦价格[30]; 整数小时; charetat[50]; } Voiture; Voiture信息[最大值]; void stockinfo(); void Saveinfo(); void Displayinfo(); void displayAll(); int n; void stockinfo() { int i; printf(“要添加多少辆车?\n”); scanf(“%d”和“&n”); 对于(i=0;i_C_Function_File_Structure - Fatal编程技术网

Displayinfo()函数不起作用,它表示文件未创建。此功能的目的是读取特定行 #包括 #包括 #包括 #定义最大值500 类型定义结构 { 字符矩阵[50]; char模型[30]; 煤焦价格[30]; 整数小时; charetat[50]; } Voiture; Voiture信息[最大值]; void stockinfo(); void Saveinfo(); void Displayinfo(); void displayAll(); int n; void stockinfo() { int i; printf(“要添加多少辆车?\n”); scanf(“%d”和“&n”); 对于(i=0;i

Displayinfo()函数不起作用,它表示文件未创建。此功能的目的是读取特定行 #包括 #包括 #包括 #定义最大值500 类型定义结构 { 字符矩阵[50]; char模型[30]; 煤焦价格[30]; 整数小时; charetat[50]; } Voiture; Voiture信息[最大值]; void stockinfo(); void Saveinfo(); void Displayinfo(); void displayAll(); int n; void stockinfo() { int i; printf(“要添加多少辆车?\n”); scanf(“%d”和“&n”); 对于(i=0;i,c,function,file,structure,C,Function,File,Structure,您正在打开文件以进行读取(“r”)。如果不存在,则不会创建该文件。您需要使用适当的模式(w或a) 从手册页: #include <stdio.h> #include <stdlib.h> #include <string.h> #define Max 500 typedef struct { char Matricule[50]; char Model[30]; char Price[30]; int KilometrePer

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

从手册页:

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

这段代码很难阅读,因为它是双倍行距的,并且没有缩进。我注意到,
main()
正在调用
main();
,这是一种糟糕的做法。请将
printf(“文件未成功创建..\n”);
更改为。您需要确定“fopen()”的确切原因失败。返回您找到的内容。好的,先生。我会这样做。为什么您的所有函数都返回
void
?如果您进一步查看,您会看到OP正在尝试读取:
fgets(T,100,save);
。关键问题是OP需要检查错误(他正在这样做-他正在检查“NULL”),然后确定错误发生的原因(例如读或打电话。
``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.