File Input Output
File Input Output
It is not enough to just display data on the screen Memory is volatile and its contents would be lost once the program is terminated.
Data organization
Our program
C Library Function
OS
Disk
File Operations
a. b. c. d. e. f. Creation of a new file Opening an existing Reading from a file Writing to a file Moving to a specific location in a file Closing a file
Opening a file
fopen() performs three important tasks then: a. b. c. It searches on the disk the file to be opened. Then it loads the file from the disk into a place in memory called buffer. It sets up character pointer that points to the first character of the buffer.
Memory Pr.c 40 Buffer Disk 40
fp
Reading a file
We have used fgetc() inside indefinite while loop, we use break statement to come out of it as we reaches end of the file at the end of the file special character EOF is inserted beyond the last character of the file.
NULL checking
If the file opening fails due to above reason fopen() returns NULL #include<stdio.h> Void main() { FILE *fp; fp=fopen(pr.c,r); If(fp==NULL) { Puts(can not open file); exit(1); } }
Closing a file
when we have finished reading from the file, we need to close it. This it is done using the function fclose() through the statement fclose(fp); When we close the file using fclose(): a. The characters in the buffer would be written to the file on the disk. b. At the end of the file a character with ASCII value 26 (EOF) would get written c. The buffer would be eliminated from memory
Writing to a file
fputc() function is similar to putchar(), However putch() always write to the VDU but fputc() write to the file.
Function
Reading from the file Writing to the file Adding new content at the end of the file Reading existing contents, writing new contents, modifying existing contents of the file. Writing new contents, reading them back and modifying existing contents of the file. Reading existing contents, appending new contents to the end of file. Cannot modify existing contents.
w+ a+
/*writes records to a file using structures*/ #include<stdio.h> void main() { FILE *fp; char another=Y; struct emp { char name[40]; int age; float bs; }; struct emp e; fp=fopen(EMPLOYEE.DAT,w); If(fp=NULL) { puts(cannot open file); exit(1); } while(another==Y); { printf(\n enter name ,age,salary); scanf(%s,%d,%f,e.name,&e.age,&e.bs); fprintf(fp,%s,%d,%f\n,e.name,e.age,e.bs); printf(add another record(Y/N); fflush(stdin); another=getche(); } fclose(fp); }
Database management
I have attempted to do this in the following menu driven program. There is a provision to add , modify, list and delete records. Following comments would help you in understanding the program easily:
Addition of record must always take place at the end of existing records in the file, much in the same way you would add new record in a register manually. Listing records means displaying the existing records on the screen. While modifying records, first we must ask the user which record he intend to modify. Instead of asking the record number to be modified, it would be more meaningful to ask for the employee whose record is to be modified. On modifying the record, the existing record gets overwritten by the new record. In deleting records, except for the record to be deleted, rest of the record must first be written to a temporary file, then the original file must be deleted, and the temporary file must be renamed back to original. Observe carefully the way the file has been opened. Clrscr() function clears the contents of the screen and gotoxy() places the cursor at appropriate position on the screen. The parameter passed to gotoxy() are column number followed by row number.
Switch(choice) { case 1: fseek(fp,0,SEEK_END); another=Y; while(another==Y) { printf(\n enter name ,age,salary); scanf(%s,%d,%f,e.name,&e.age, &e.bs); fwrite(&e,sizeof(e),1,fp); printf(add another record(Y/N); fflush(stdin); another=getche(); } break;
Case 2: rewind(fp); while(fread(&e,resize,1,fp)==1) printf(\n%s%d%f,e.name,e.age,e.bs); Break; Case 3: Another=Y; While(another==Y) { printf(\nenter name of employee to modify); scanf(%s,empname); rewind(fp); while(fread(&e,resize,1,fp)==1) { if(strcmp(e.name,empname)==0) { printf(\nenter new name age , bs); scanf(%s%d%f,e.name,&e,age,&e.bs );
rename(TEMP>DAT,EMP.DAT) ; fp=fopen(EMP.DAT,rb+);
printf(Delete another record(Y/N); fflush(stdin); another=getche(); } break; Case 0: fclose(fp); exit(); } } }
} Break; Case 4: Another=Y; While(another==Y) { printf(\nenter name of employee to delete); scanf(%s,empname); rewind(fp); while(fread(&e,resize,1,fp)==1) { if(strcmp(e.name,empname)!=0) { fwrite(&e,resize,1,ft); } fclose(fp); fclose(ft); remove(EMP.DAT);