Binary file- menu writing and reading, in c issue
NickName:Dimitar Bochev Ask DateTime:2016-03-16T18:05:54

Binary file- menu writing and reading, in c issue

I have to write a program for writing and reading a binary file. There should be a menu for the choice. The file should have indentification number, an article with name, parameters, etc and a price. So it has to be int|sizeof char|char|double.

I have some issues with the code. It compiles. I open the file and write in it. But it "dont want" to read itself. When I remove the double slash comments to make the check the program prints the errors. I need help.

This is the code:

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

}

Ok, so i edited the parenthesis and it works. But without the checks. Thats the reason i commended them. It seems i have problem with them, can u give me more info about the checks in C and help me get these ones work. These are the warnings warnings

*Edit2 I typed the code from the zero again and it works. I fixed the checks.

Copyright Notice:Content Author:「Dimitar Bochev」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36032428/binary-file-menu-writing-and-reading-in-c-issue

More about “Binary file- menu writing and reading, in c issue” related questions

Binary file- menu writing and reading, in c issue

I have to write a program for writing and reading a binary file. There should be a menu for the choice. The file should have indentification number, an article with name, parameters, etc and a pric...

Show Detail

Reading and writing binary file in C

Firstly, I read here: reading-binary-files-in-c binary-file-reading-writing-in-c I am trying to read binary file in C. And wrote this code: struct emp { char name[20]; char surname[20]; ...

Show Detail

Binary Reading from and Writing to a File in C

I need some help writing a program in C to reading and writing binary values from and to a file. For this program I do not need the entire contents of the file (2048 bytes) read in, only the first 30

Show Detail

Issue writing/reading a uint to a binary file

I am writing a struct to a binary file in C. The char* items and the uint8 items are writing fine, but i seem to be having an issue writing the uint32 item. my writing code is here. void writeOut(

Show Detail

Reading and Writing from a binary file in C

This question has been asked many times before, but I've been trying this for hours with a variety of methods and I have no clue why this is wrong. I've been writing C structs to a binary file us...

Show Detail

Differences between writing/reading binary/text in c

I'm working on a client/server program where the client sends/receives files. The files may be text files or binary files. However, I am not sure what changes I need to make, if any, to accommodate...

Show Detail

How to resolve segmentation error in reading/writing to binary file in C

This is the struct definition that I am trying to write copies of to and read from binary file typedef struct carType Car; struct carType { int vehicleID; char make[20]; char model[20]...

Show Detail

qt binary file writing and reading

void write(QString filename) { QChar ch('b'); QFile mfile(filename); if (!mfile.open(QFile::WriteOnly) { qDebug() &lt;&lt; "Could not open file for writing"; return; } QDataStream..

Show Detail

Writing/reading strings as binary into random accessed file

Simple question: I want to write strings of fixed length into a binary file (that is, as binary), as illustrated in the following snippet. The writing &quot;looks&quot; fine, but reading from the f...

Show Detail

C program reading and writing to binary files

Hello this is the first time I am working with binary file in C. I am trying to first write the data to the file and then I to read the same data from the file. But somehow I am not reading the data

Show Detail