Reputation: 65
What I want to do in my program is to add a person ( or more ) and thereafter see the amount of bytes used, when going to (2), case 2. My #include "header.h" is:
struct student {
long long int personalnumber;
char name[20];
char gender[20];
char program[20];
int age;
char email[20];
};
What I do here is start the program, click 1, write all the information the program asks for, maybe add two students (therefor the loop). when I click (n) the program should go back to the options. When Clicking 2 the amount of bytes used in the binary file should be printed out. Main function is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
int main()
{
int val, age;
long long int personalnumber, size;
char email[20], program[20], gender[20], name[20], n;
do
{
system("cls");
printf("1: Add\n\n");
printf("2. info\n\n");
scanf("%d", &val);
getchar();
switch (val)
{
case 1:
system("cls");
printf("Add a new student to the database\n");
struct student s1;
FILE *myfile;
myfile = fopen("students.dat", "wb");
if (myfile == NULL)
{
printf("Could not open the file!\n");
system("pause");
exit(1);
}
while (1)
{
printf("\nEnter student personalnumber: ");
scanf("%lld", &s1.personalnumber);
getchar();
printf("Enter name of student: ");
fgets(&s1.name, 20, stdin);
printf("Enter the gender of the student: ");
fgets(&s1.gender, 20, stdin);
printf("Enter the Program of the student: ");
fgets(&s1.program, 20, stdin);
printf("Enter the Age of the student: ");
scanf("%d", &s1.age);
getchar();
printf("Enter the students Email: ");
fgets(&s1.email, 20, stdin);
myfile = fopen("students.dat", "wb");
fwrite(&program, sizeof(struct student), 1, myfile);
printf("Do you wish to add more students? Enter y or n");
n = getchar();
getchar();
if (n == 'n')
{
break;
}
}
fclose(myfile);
system("pause");
break;
case 2:
freopen("students.dat", "rb", myfile);
system("cls");
printf("bytes:\n\n");
myfile = fopen("students.dat", "rb");
fseek(myfile, 0, SEEK_END);
size = ftell(myfile);
rewind(myfile);
printf("Size of file is: %lld", size);
system("pause");
break;
}
} while (val != 3);
system("pause");
return 0;
}
Upvotes: 0
Views: 79
Reputation: 25286
With myfile = fopen("students.dat", "wb");
in the loop, you open the file again and again. Then you write to the file, overwriting anything that was in the file, so the file only contains your last write. Probably (possibly) your second call of fopen
fails as the "file is already open by another process" but since you don't check the return value of the open call, you don't notice.
I suggest you read about the fopen
call and especially the open modes.
As for hints (without providing a full solution): place the open call before the loop. You may have to check whether the file exists and decide whether to truncate the file or open it in read/write mode. Likewise, place the fclose
call after the loop, so you only close it after you are done working with it.
You must also check the return value of open to know whether it has suceeded or not.
program
but tell the write call it has the size of struct student
. That isn't correct. Also, to pass a pointer to an array to a function, you only need to mention the name of the array; the compiler will translate this into the address of the array, so you don't need to use the address-of operator (&
).
case 2:
you make the same mistake of opening the file multiple times (and of not checking whether the calls succeeded). freopen
closes the file associated with the handle (that file is already closed, so this could invoke an error) and opens the new file. Then you call fopen
on the file again, probably causing an error. At the end of the case
you must close the file.
Upvotes: 1