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

标签 c function file structure

#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/

相关文章:

c - 使用 IP_ADD_SOURCE_MEMBERSHIP 无法为 RAW 套接字添加超过 10 个源地址

c - 如何链接 glibc 的 iconv 实现?

c - 为什么我需要在同名的 .c 文件中包含一个 .h 头文件?

function - fish shell : Check if argument is provided for function

java - 如何检查二维文件

file - 加载所需文件,相对路径与绝对路径

c++ - 优化 O_DIRECT 写入

python - 评估Python中字串的辅音/元音组成

php - 已弃用:mysql_connect()

c - 逐页显示文件