hi everyone
i am trying to write a program for a library system that allow stuff to add, remove, view and delete customer. i try to use file to store data and i open the file in mode read then i store then i put the content of the file into a struture. now the problem started it is only showing me haft of the content and here is the coding.

#include <stdio.h>
# include <windows.h>
# include <stdlib.h>
#include<string.h>
#include<conio.h>

void transaction();
void Book();
void view(int x,struct books eli[20]);


struct books{
char name[22], book[25],date1[10], date2[10],id[7],bid[7];

};


int main(void)
{

    struct books eli[20];
    int num,ctr=0,index=0;

    FILE *fp;
    fp = fopen("database.txt","r");

  for(ctr=0;fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2)!=EOF;ctr++)
    {

    fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2);

     index=ctr+index;
    }
        fclose(fp);


  do
     {
       system("cls");  
         printf("\n\t\t***************************************");
         printf("\n\t\t\t\t\t\t\t\t\t\t\t\t\t Welcome To BOOK MENU ");
         printf("\n\t\t***************************************\n");

         printf("\n\t\t1. View Books");
         printf("\n\t\t2. Exit");
         printf("\n\t\t***************************************");
         printf("\n\t\t Choice:");
         scanf("%d", &num);

         switch(num)
         {

            case 1:
            view(index,eli);

                break;
            case 2:
                return 0;   
                break;
             default :
                printf(" \n\t\t wrong value !!!");
                break;
         }


   }while(num!=2);


         Sleep(2000);
    }

void view(int x,struct books eli[20])
{
    printf("%d",x);
    int ct;
for(ct=0;ct<x;ct++)
    {
        printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",eli[ct].name,eli[ct].id,eli[ct].book,eli[ct].bid,eli[ct].date1,eli[ct].date2);
}
Sleep(5000);
}

Edited 2 Years Ago by bogoreh

delete line 30, it's already reading the file in line 27.

Post the function that writes the file.

A lot easier way to do it is to read/write binary files instead of text files using fwrite() and fread(). You write the entire structure as one chunk that way. But don't expect to view the file with a text editor such as Notepad.

thks a lot i will follow your advise and i will try to do with binary file. and again thanks.

This article has been dead for over six months. Start a new discussion instead.