user956281
user956281

Reputation: 23

C prog: Write to and from binary files not working?

I've got a question concerning C programming.It is concerning binary files.In my code I(at least imagined that I) made it read and write from a binary file,but it won't do that for some reason.I've tried changing some things but I just don't see the error anywhere.And it is just this minimal detail which is giving me a hard time,it is all that is left to make in this code before it is done and then I'm done with the program altogether,so it gets on my nerves a bit more than it would usually.Please point out what it is I haven't done and what i should do to make it read from and write to binary files properly:

 #define MAX 4

//========================-STRUCTURE-=====================================================================

struct person{

char name[30];
char mail[30];
char tele[30];

};

struct person p[MAX];

//========================-MAIN FUNCT-===============================================================
int main()
{

bool continue=true,exists=false;
int answer,i;
char filnme[20];

FILE *fil;

printf("Would you like to open an already existing file?[y/n]: ");
scanf("%d",&answer);
if(answer=='y'||answer=='Y')
{
    finns=true;
}
else finns=false;

getchar();

printf("\n\nType the name of the file you will use: ");
scanf("%s",filnme);

if(exists)
{

    fil = fopen(filnme,"rb");

    for(i=0;i<MAX;i++)
    {
        while(feof(fil)==0)
            {
            fread(&p, MAX, 1, fil);
            printf("%s %s %s\n",p.name,p.mail,p.tele);
        }

    }

    fclose(fil);
}

else fil = fopen(filnme,"wb");

do
{

    system("cls");

    int choice;

    printf("What would you like to do?");
    printf("\n_______________\n");
    printf("1. Add another person to the list\n");
    printf("2. Print the entire list\n");
    printf("3. Remove information from the list\n");
    printf("4. Sort\n");
    printf("5. Change information\n");
    printf("6. Search\n");
    printf("7. Exit\n");
    printf("\nChoice: ");
    scanf("%d",&choice);
    printf("\n");

    system("cls");

    switch(choice)
    {

        case 1:

        addtolist();
        getch();
        break;

        case 2:
        write();
        getch();
        break;

        case 3:
        remove();
        getch();
        break;

        case 4:
        sort();
        getch();
        break;

        case 5:
        change();
        getch();
        break;

        case 6:
        search();
        getch();
        break;

        case 7:
        continue=false;
        break;

        default:
        printf("Not a valid choice!");
        getch();
        break;

    }

}while(continue);

for(i=1;i<=MAX;i++)
{

        fwrite(&p, MAX, 1, fil);

}

fclose(fil);

system("cls");

}

Nevermind the declaration of functions or such,I've got that covered,just the binary file handling I put into the main.

Thanks in advance!

Upvotes: 0

Views: 812

Answers (3)

Jens
Jens

Reputation: 72707

Apart from other things, scanf("%d", &answer) attempts to read a number, not a y/n character.

Upvotes: 0

selbie
selbie

Reputation: 104559

continue is a reserved keyword in C/C++ - don't use that as a boolean variable name.

finns is not declared (assumed to be bool)

You aren't checking the return value of fopen. How do you know if the file was successfully opened?

Your while loop on fread does't look anywhere near right.

This looks much better

int records_read = 0;
size_t count;

count = 0;
for (i = 0; i < MAX; i++)
{
    count = fread(&p[i], sizeof(p[i]), 1, fil);
    if (count == 0)
    {
        break;
    }
    records_read++;
}

Same for your write loop (note that I adjusted the starting value of "i")

for(i=0;i<MAX;i++)
{
    fwrite(&p[i], sizeof(p[i]), 1, fil);
}

Upvotes: 0

NPE
NPE

Reputation: 500703

You are not using fread and fwrite correctly.

fread(&p, MAX, 1, fil);

should be:

fread(&p[i], sizeof(person), 1, fil);

Similarly for the fwrite call.

An alternative to calling fread and fwrite in a loop is to read/write the entire array in one go, like so:

fread(&p, sizeof(person), MAX, fil);

Upvotes: 2

Related Questions