Is it better now?
insert
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book
{
char title[140];
char category[50];
int edition;
int publish_year;
double price;
};
int main()
{
struct Book book;
FILE *myfile = fopen("./my-data.dat", "rb");
if (myfile == NULL) {
printf("Could not open the file!\n");
return 1;
}
FILE *file1 = fopen("./file1.dat", "wb");
FILE *file2 = fopen("./file2.dat", "wb");
FILE *file3 = fopen("./file3.dat", "wb");
if (file1 == NULL && file2 == NULL && file3 == NULL) {
printf("Could not open the file!\n");
return 1;
}
while (fread(&book, sizeof(struct Book), 1, myfile))
{
if(strcmp(book.category, "Software Engineering") == 0 && book.publish_year > 2005)
{
fwrite(&book, sizeof(struct Book), 1, file1);
}
else if(strcmp(book.category, "Software Engineering") == 0 && book.publish_year < 2005)
{
fwrite(&book, sizeof(struct Book), 1, file2);
}
else
fwrite(&book, sizeof(struct Book), 1, file3);
}
fclose(file1);
fclose(file2);
fclose(file3);
fclose(myfile);