OUTLINE We will design a sample “Library” database in C language.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Lecture 19: Using structures in C Language Computer Programming Structures in C Language Lecture 19.
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
C Language.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
1 Structure part 1 & File Processing. 2 Structures.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
CPS120: Introduction to Computer Science Functions.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
FTP Server API Implementing the FTP Server Registering FTP Command Callbacks Data and Control Port Close Callbacks Other Server Calls.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Computer Programming Control Structure
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
C LANGUAGE Characteristics of C · Small size
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
FTP Client API FTP in embedded devices Implementing an FTP Client FTP Command APIs Other FTP Client APIs.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Dr. Sajib Datta Jan 16,  The website is up.  Course lectures will be uploaded there ◦ Check regularly for assignments and update.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 1.2 Introduction to C++ Programming
Review 1.
Chapter 1.2 Introduction to C++ Programming
MT262A Review.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
EKT120: Computer Programming
Chapter Topics 11.1 Introduction to Menu-Driven Programs
CS111 Computer Programming
Introduction to Programming
FILE HANDLING IN C.
Govt. Polytechnic,Dhangar
EPSII 59:006 Spring 2004.
C Programming - Lecture 3
Files Chapter 8.
Presentation transcript:

OUTLINE We will design a sample “Library” database in C language. For permanent recording, we will use FILING. For memory recording, we will use arrays.

Introduction to Databases Database is a kind of Software which can perform different operations (functions) on a large group of records (Transactions). Example of operations include:- Add transaction, delete transaction, update (modify) transactions, search transactions. Where Databases are used:- Database software are frequently used in Banks, Hospitals, Post offices, Weather Departments, Government offices, Libraries, Universities etc. Why we need Databases:- For future purposes. Remember: Past is the best predict of future.

Our Library Database System Will perform the following operations:- User can add a book in the Database. User can delete a book in the Database. User can modify a book in the Database. User can list all the books in the Database. User can search any book in the Database.

Press key to select options:- Press ‘1’ to add new book. Press ‘2’ to delete a book. Press ‘3’ to modify a book. Press ‘4’ to list all the books. Press ‘5’ to search the book. Press ‘Esc’ to exit the system. User key = 1 USER has selected add new book option. ---------------------------------------- Enter the ISBN:- Enter the name:- Enter the author name:- --

Library Database system Flow chart start Read database from FILE and store in array (memory) Add new book Delete book Get key Modify book key List books Search book Store database from array to FILE Exit

Selection of Data structures ISBN Book Name Author Name Publisher Number of Copies Year of Publish 1293 Network Security Martin Waley 4 1998 9382 Data mining Muhammad Zaki Wrox 6 2003 9993 Data warehousing Stephen Brobst MIT 8 --- int Copies [ ] int ISBN [ ] char AuthorName [ ][30] int PYear [ ] char Publisher [ ][30] char BookName [ ][30] Our array will be static. We have to specify a certain size at Compile time and it will not change at Execution time (run time).

Transactions variables int ISBN [200]; char BookName [200][30]; char AuthorName [200][30]; char PublisherName [200][30]; int Copies [200]; int PYear [200]; int TransactionCount = 0;

Header files and function declaration…… void main (void) { clrscr(); ReadDatabase (); char keyPress; while (1) menu (); cout << “USER key = “; keyPress = getche (); switch (keyPress); case ‘1’: AddBook (); break; case ‘2’: DeleteBook (); break; case ‘3’: ModifyBook (); break; case ‘4’: ListBooks (); break; case ‘5’: SearchBook (); break; case 27: EndSystem (); break; }// end of switch statement } // end of while statement } void menu ( ) { cout << “Press any key to select options:-\n”; cout << “Press ‘1’ to add a new book\n”; cout << “Press ‘2’ to Delete a book\n”; cout << “Press ‘3’ to Modify a book\n”; cout << “Press ‘4’ to List all the books\n”; cout << “Press ‘5’ to Search a book\n”; cout << “Press ‘Esc’ to Exit the system\n”; }

Add new Book function (Implementation) void AddBook ( ) { cout << “USER has selected Add new book option.”; AddBook (TranactionCount); TransactionCount++; } void AddBook (int TransactionID) cout << “\nEnter ISBN: “; cin >> ISBN [TransactionID]; cout << “\nEnter Book Name :”; cin >> BookName [TransactionID]; cout << “\nEnter Author Name:”; cin >> AuthorName [TransactionID]; cout << “\nEnter publisher Name”; cin >> PublisherName [TransactionID]; cout << “\nEnter number of copies:”; cin >> Copies [TransactionID]; cout << “\n Enter publishing year: “; cin >> PYear [TransactionID];

Delete Book function (Implementation) void DeleteBook (void) { int tempISBN = -1; cout << “USER has selected Delete a book option.”; cout << “\nEnter ISBN of Book for deleting: “; cin >> tempISBN; for ( int loop = 0; loop < 200; loop ++ ) if ( tempISBN == ISBN [loop] ) { ISBN [loop] = -1; tempISBN = -2; break; } } if ( tempISBN != -2 ) cout << “\nBook not found.”; else cout << “\nBook deleted successfully”;

Modify Book function (Implementation) void ModifyBook (void) { int tempISBN = -1; cout << “USER has selected Modify a book option.”; cout << “\nEnter ISBN of Book for Modify: “; cin >> tempISBN; for ( int loop = 0; loop < 200; loop ++ ) if ( tempISBN == ISBN [loop] ) tempISBN = -2; break; } if ( tempISBN != -2 ) cout << “\nBook not found.”; else AddBook (loop);

List books function (Implementation) void ListsBook (void) { int tempISBN = -1; cout << “USER has selected List Books option.”; for ( int loop = 0; loop < 200; loop ++ ) if ( ISBN [loop] != -1 ) cout << “\nISBN : “<< ISBN [loop]; cout << “\nBook name : “<< BookName [loop]; cout << “\nAuthor name : “<< AuthorName [loop]; cout << “\nPublisher name : “<< PublisherName [loop]; cout << “\nNumber of copies : “<< Copies [loop]; cout << “\nYear of publish: “<< PYear [loop]; }

Search Book function (Implementation) void SearchBook (void) { char tempBookName[200]; cout << “USER has selected Search a book option.”; cout << “\nBook name to search: “; cin >> tempBookName; for ( int loop = 0; loop < 200; loop ++ ) if ( strcmp (tempBookName, BookName[loop] ) == 0 ) cout << “\nISBN : “<< ISBN [loop]; cout << “\nBook name : “<< BookName [loop]; cout << “\nAuthor name : “<< AuthorName [loop]; cout << “\nPublisher name : “<< PublisherName [loop]; cout << “\nNumber of copies : “<< Copies [loop]; cout << “\nYear of publish: “<< PYear [loop]; }

End System function (Implementation) void EndSystem (void) { FILE *fptr; fptr = fopen (“d:\\LibraryData.txt”, “w”); for ( int loop = 0; loop < 200; loop ++ ) if ( ISBN [loop] != -1 ) // only add valid books fprintf (fptr, “%d %s %s %s %d %d”, ISBN [loop], BookName [loop], AuthorName [loop], PublisherName [loop], Copies [loop], PYear [loop] ); } fclose (fptr); exit (1);

Read Database function (Implementation) void ReadDatabase (void) { FILE *fptr; fptr = fopen (“d:\\LibraryData.txt”, “r”); while (1) { int status = fscanf (fptr, “%d %s %s %s %d %d”, &ISBN [TransactionCount], BookName [TransactionCount], AuthorName [TransactionCount], PublisherName [TransactionCount], &Copies [TransactionCount], &PYear [TransactionCount] ); TransactionCount ++; if ( status == EOF ) break; } fclose (fptr);

File and Directory Operations C provides a number of file/Directory handling operations. Remove file Rename file Make directory Delete directory Search files Execute files

Removing file { int flag = remove (“c:\\first.bmp”); if ( flag == 0 ) We can remove any file using int remove (char *filename) function. On success, function returns 0, while on error function returns -1. Include the file “stdio.h”, before using this function. { int flag = remove (“c:\\first.bmp”); if ( flag == 0 ) cout << “File deleted successfully”); else cout << “Can’t remove the file”); }

Rename file { int flag = rename (“c:\\first.bmp”, “c:\\second.bmp”); We can rename any file using int rename (char *oldname, char *newname) function. On success, function returns 0, while on error function returns -1. Include the file “stdio.h”, before using this function. { int flag = rename (“c:\\first.bmp”, “c:\\second.bmp”); if ( flag == 0 ) cout << “File deleted successfully”); else cout << “Can’t remove the file”); }

Make/Remove Directory We can make/remove any directory using int mkdir (char *filename) and int rmdir (char *filename) functions. On success, function returns 0, while on error function returns -1. Include the file “dir.h”, before using this function. { int flag = mkdir (“c:\\mydirectory”); if ( flag == 0 ) cout << “Directory created successfully”); else cout << “Can’t create the directory”); }

File searching struct ffblk; int findfirst(char *search_criteria,struct ffblk *,int argument); int findnext(struct *ffblk); These all functions are declared in “dir.h” file.

File searching void main ( void ) { struct ffblk filesearch; int done; done = findfirst(“c:\\*.*",&filesearch,0); while (done != -1) printf(" %s\n", filesearch.ff_name); done = findnext(&filesearch); }

Executing Files We can execute any “exe” file using int system (char *filename) function. On success, function returns 0, while on error function returns -1. Include the file “system.h” and “process.h”, before using this function. { system (“dir”); system (“c:\\first.bmp”); system (“notepad”); }