-1

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)
IrAM
  • 1,720
  • 5
  • 18

2 Answers2

0

As already pointed out by people in comments, you should try to use fgets() for reading file and then sscanf for reading integers from the string. Here is an example:

int 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);
    }

    char input[100];
    char *result;
    while ((result = fgets(input, 100, filePtr)) != NULL) {
        // Book Name
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        strcpy(book[i].BookName, input);

        // Book ISBN
        result = fgets(input, 100, filePtr);
        if (result == NULL) break;
        sscanf(input, "%d", &book[i].BookISBN);

        // Book Borrowed
        result = fgets(input, 100, filePtr);
        if (result == NULL) break;
        sscanf(input, "%d", &book[i].Borrowed);

        // Book Borrower Name
        result = fgets(input, 100, filePtr);
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        if (result == NULL) break;
        strcpy(book[i].BorrowerName, input);

        // Book Field
        result = fgets(input, 100, filePtr);
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        if (result == NULL) break;
        strcpy(book[i].Field, input);

        i++;
    }

    fclose(filePtr);
    return i;
} 

When I run this code on your file, I'm able to see documents being printed:

c-posts : $ gcc booking.c 
c-posts : $ ./a.out 
Book Name is : Technique Informatique
Book ISBN is : 90023
Borrowed is : 1
Borrower Name is : Adam Ridge 
Field is : special

Book Name is : Data Structures
Book ISBN is : 23451
Borrowed is : 0
Borrower Name is : None 
Field is : Computer Science 

Book Name is : E-commerce Blockchain 
Book ISBN is : 14678
Borrowed is : 1
Borrower Name is : Adam Ridge
Field is : Business 

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

Well you should be using fgets for reading string, as other people have already said.

Nevertheless, and to answer you question, here is some advice that might help you:

  1. Book structure contains char arrays (char []) not pointers (char *), so reading into those variables does nor have the & symbol, e.g.:

    fscanf(filePtr, "%[^\n]%d\n%d\n%[^\n]%*c%[^\n]%*c", book[i].BookName...) != EOF)

  2. The correct format of the fscanf function for this case is:

fscanf(filePtr, "%[^\n]%d\n%d\n%[^\n]%*c%[^\n]%*c", book[i].BookName, &book[i].BookISBN, &book[i].Borrowed, book[i].BorrowerName, book[i].Field) != EOF)

Where:

a) %[^\n] -> reads a full line

b) %d\n -> reads an int and an end of line

c) [^\n]%*c -> reads a full line and any extra chars. Note, that if you use * in the format string, it suppresses assignment:

%*c = read 1 character, but don't assign it to any variable

lihudi
  • 860
  • 1
  • 12
  • 21