二进制文件 - 菜单写入和读取,在c问题

时间:2016-03-16 10:05:55

标签: c file menu binary

我必须编写一个用于编写和读取二进制文件的程序。应该有一个菜单供选择。该文件应具有识别号,具有名称,参数等的文章和价格。所以它必须是int | sizeof char | char | double。

我的代码存在一些问题。它汇编。我打开文件并写入。但它“不想”读自己。当我删除双斜杠注释以进行检查程序打印错误。我需要帮助。

这是代码:

#include <stdio.h>

void writing();
void reading();
int main ()
{
    char choice;

    /* do
    {
        printf("Enter 1 for writening in file and 2 for reading!\n");
        scanf("%c", &choice);
       // choice=getchar();
        fflush(stdin);
    }
    while(choice!='1'&&choice!='2');
    switch(choice)
    {
        case '1':writing(); break;
        case '2':reading(); break;
    } */

    for (;;)
    {
        printf("Enter a number\n");
        printf("Choose 1 for writening and 2 for reading\n");
       //choice=getchar();
        scanf("%c", &choice);
        fflush(stdin);
       if (choice=='1'||choice=='2')
       break;
    }
        switch(choice)
        {

        case '1': writing (); break;
        case '2': reading (); break;
        }


    return 0;
}
void writing ()
{
    int j,i; // size of article
    int number;
    char article[50];
    double price;

    FILE*fp;

    printf("Enter a string less than 50\n");
    gets(article);

    //scanf("%c", &article);

    j=(sizeof (article));

    printf("Enter a number:\n");
    scanf("%d", &number);

    printf("Enter a price:\n");
    scanf("%lf", &price);

    if (fp=fopen ("writing.data","ab")==NULL)
    {
        printf("Couldnt open the file\n");
    }

    fwrite(&number,sizeof (int),1,fp);

   /* if(fwrite(&number,sizeof (int),1,fp)!=1)
    {
       printf("Error\n");
    } */
    fwrite(&j,sizeof(int),1,fp);

    /* if(fwrite(&j,sizeof (int),1,fp)!=1)
    {
       printf("Error\n");
    } */
      fwrite(article,sizeof (article),1,fp);

    /*  if(fwrite(article,sizeof (article),1,fp)!=1)
    {
       printf("Error\n");
    } */

    fwrite(&price,sizeof (double),1,fp);

    /*if(fwrite(&price,sizeof (double),1,fp)!=1)
    {
       printf("Error\n");
    } */

    fclose(fp);
}

void reading()
{
    int i; // size of article
    int number;
    int number1;
    char article[50];
    double price;

    FILE*fp;

    printf("Enter a number to start the reading:");
    scanf("%d", &number1);
    if (fp=fopen ("writing.data","rb")==NULL)
    {
        printf("Couldnt open the file\n");
    }
    for (;;)
    {
       fread(&number,sizeof(int),1,fp);

      /*  if(fread(&number,sizeof(int),1,fp)!=1)
        {
            printf("Error");
            break;
        } */

        fread(&i,sizeof(int),1,fp);

       /* if(fread(&i,sizeof(int),1,fp)!=1)
        {
            printf("Error");
            exit(1);
        } */

        fread(article,sizeof(article),1,fp);

       /* if(fread(article,sizeof(article),1,fp)!=1)
        {
            printf("Error");
            exit(2);
        } */

        fread(&price,sizeof (double),1,fp);

        /* if(fread(&price,sizeof (double),1,fp)!=1)
        {
            printf("Error");
            exit(3);
        } */

        if(number==number1)
        {
            printf("%d", number);
            printf(article);
            printf("%lf", price);

        }
}
 fclose(fp);

}

好的,所以我编辑了括号并且它有效。但没有检查。这就是我赞扬他们的原因。看来我对他们有问题,你能不能给我更多关于C中支票的信息,并帮我搞定这些。 这些是警告warnings

* EDIT2 我再次输入零码的代码,它的工作原理。我修好了支票。

1 个答案:

答案 0 :(得分:0)

这是错误的:

if (fp=fopen ("writing.data","rb")==NULL)

您忘了正确地放置括号。替换为:

if ((fp=fopen ("writing.data","rb"))==NULL)

编译所有启用的警告,编译器应该给你一个警告。

还有其他问题,例如:

if ((fp=fopen ("writing.data","rb")) == NULL)
{
    printf("Couldnt open the file\n");
    // you should exit here, it's pointless to continue
}
...

如果在fpNULL时继续执行,则最终会出现fread,其中文件指针为NULL,这会导致未定义的行为(很可能是您的程序)会崩溃。)

相关问题