I'm working on a project of booking , so the idea of project when the program starts it should read a data from a file called databook and save them on the struct , and everytime i'm adding a book it should add to program
the difficult i found the name has a space between name and nickname so i used scanf but the problem is scanf don't read full line i used scanf("%[^\n]s",)
but it doesnt work
the source code below to understund more
#include <stdio.h>
#include <stdlib.h>
#define MAX_ARRAY_SIZE 5
typedef struct Book
{
char BookName[50];
int BookISBN;
int Borrowed;
char BorrowerName[50];
char Field[50];
}Book;
Book book[MAX_ARRAY_SIZE];
void ReadFile(char* fileName);
int main(int argc, char* argv[])
{
char* fileName = "c1.txt";
ReadFile(fileName);
int i = 0;
for (i = 0; i < MAX_ARRAY_SIZE; i++)
{
printf("Book Name is : %s\n", book[i].BookName);
printf("Book ISBN is : %d\n", book[i].BookISBN);
printf("Borrowed is : %d\n", book[i].Borrowed);
printf("Borrower Name is : %s\n", book[i].BorrowerName);
printf("Field is : %s\n", book[i].Field);
printf("\n");
}
exit(EXIT_SUCCESS);
}
void ReadFile(char* fileName)
{
FILE* filePtr = NULL;
int i = 0;
if ((filePtr = fopen(fileName, "r")) == NULL)
{
printf("Error : Unable to open %s for reading\n");
exit(EXIT_FAILURE);
}
while (fscanf(filePtr, "%s%d%d%s%s", &book[i].BookName, &book[i].BookISBN, &book[i].Borrowed,&book[i].BorrowerName,&book[i].Field) != EOF)
{
i++;
}
fclose(filePtr);
}
for databook
Technique Informatique //BookName1
90023 //BookISBN1
1 //(OR O) - means 'Borrowed OR not
Adam Ridge //BorrowerName1 (None in case Not borrowed)
special//(field)
Data Structures //BookName1
23451 //BookISBN1
0 //(OR O) - means 'Borrowed OR not
None //BorrowerName1 (None in case Not borrowed)
Computer Science //(field)
E-commerce Blockchain //BookName1
14678 //BookISBN1
1 //(OR O) - means 'Borrowed OR not
Adam Ridge //BorrowerName1 (None in case Not borrowed)
Business //(field)