Monday, June 3, 2024

Part 4: Library management in C| Pagination, Dynamic Memory allocation, write Comma Separated Values (CSV) file

Previous Page

In this part, we will create pagination in C programming to view large set of books data. In college there might be thousands of books. We can't display all items on the screen, so we need to create techniques to display small set of data at a time and allow users to select next page or previous page to view data which is called pagination. Also, we will discuss to manage large set of books data and write books data into CSV file.

Here, we will discuss following items:

  • Pagination: Users can explore large set of data by selecting next or previous page of data.
  • Dynamic Memory allocation: Dynamic numbers of books are loaded into the memory, modify or delete book details from the memory using dynamic memory allocation.
  • Write Comma Separated Values (CSV): Export large set of books data into CSV file.
Please follow the source code of header file userMenu.h and loadCsvData.h. 

We have three main functions to implement pagination features. The main technique is to load data into computer memory (RAM). The number of data which can be loaded in memory depend upon size of the memory. However, we will not get memory problem to load couple of thousands of data. In this application I haven't used large set of data however this technique can be used for any size of data.
The name of the functions are readBooks(), viewAllBooks(), and viewBooksByPage().  

int readBooks(): This function is written inside loadCsvData.h header file. This function is called at the very first start of the application inside void adminMenu() and void studentMenu().

  1. Line 148: int bkSize = getRowCount(fpl), function getRowCount() reads the books data file to count number of books and assign count to the variable bkSize. This variable is used to allocate size of the array of book structure. 
  2. Line 151: rewind(fpl); this will rewind the file pointer back to start of the file to continue reading books data.
  3. Line 151: bk = (struct books*)malloc(bkSize * sizeof(struct books)); using malloc function we will allocate the size of the array bk equal to number of books count, and bk is array pointer.
  4. Remaining codes up to line 194 will read books data from the file using loadBookValues functions which is discussed in part 3 of this tutorial. 
  5. Line 196: bk++, this will increment the pointer by 1 unit so that we can assign new books value into the array.
  6. Line 201: bk=(bk-bkSize), this will move pointer back to initial position so that we can view data from the beginning of array. 

void viewAllBooks(int flag): This function calculates the value of lower index and higher index of an array based on user input to display books data on the screen. The parameter flag will decide whether it is viewed by student or admin user. The number of data displayed per page is 15 which is declared by variable pageSize. At first lower index is zero, and higher index is 15 which is defined by variables lIndex and hIndex respectively. Now, when user will press key 'b' to view next page, the lower and higher index will be incremented by 15, if the value of higher index is greater than books count then higher index will be equal to books count, stops incrementing variables value. In similar way, if user press key 'n' to view previous page, the initial and lower index will be subtracted by 15. If lower index value will be less than zero, then initial index is set to zero and stops decrementing variables value. Every time user press key, the calculated initial index and higher index is passed to function viewBooksBypage() to display books data on the screen.

Add, Update and Delete books data: As books data is already loaded in memory data, modifying and deleting data do not involve C file operations. Add, update and delete operation happens directly in data array. Here dynamic memory allocation plays major role to perform these operations.
  • Update book data: We can update the value of array directly which is very fast and easy. The function updateBook() is created to update books data. It requests book's new details from the user and update the value in array.
  • Add book data: We can't simply add additional elements in array because memory is not already allocated. At first, we have to allocate memory and add new elements. The function addBook() is created to add books data. It requests new books information and add books data in the array.
    • Line 375: int newbkCount=bkCount+1;, assign variable by new book count.
    • Line 377: bk = (struct books*)realloc(bk,newbkCount * sizeof(struct books));, the memory of array bk is reallocated using realloc function by new new book count.
    • Line 378: bk = (bk+bkCount);, move the pointer to new element of an array.
    • Line 380 to 386: Copy the books value into bk array using strcpy function.
    • Line 388: bk = (bk-bkCount), move the pointer back to initial position of an array.
    • Line 389: update the variable bkCount by new count i.e newbkCount. 
  • Delete book data: The logic to delete data is to allocate new memory block with size 1 item less than current memory block. Copy all data to new memory block except the item which we want to delete. Free previous memory block and point pointer to new memory block.
    • Line 475: tmp_bk = (struct books*)malloc((bkCount-1) * sizeof(struct books)), allocate new memory block of size bkCount-1.
    • Line 477: if(indexToRemove != 0) memmove(tmp_bk,bk,(indexToRemove)*sizeof(struct books)), copy data from memory bock bk to tmp_bk upto the block which we want to delete.
    • Line 480: if(indexToRemove != (bkCount-1)) memmove(tmp_bk+indexToRemove,bk+(indexToRemove+1),(bkCount-indexToRemove-1)*sizeof(struct books)), copy data from memory bock bk to tmp_bk after the block which we want to delete.
    • Line 483: free(bk), free memory block.
    • Line 484: bk = tmp_bk, assign the address of new memory block to old pointer bk.
Write data into Comma Separated Values (CSV) file
The method to write data into CSV file is straight forward and easy. We can use fprintf() function to write data in file as a required format.

No comments:

Post a Comment