Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26.

Similar presentations


Presentation on theme: "Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26."— Presentation transcript:

1 Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26

2 Lecture 26: FILING HANDLING in C Language OUTLINE Now we will learn What are the FILES in C language. 2 different types of FILES. Why we need FILES. How we can create/write/read or close any FILE.

3 Lecture 26: FILING HANDLING in C Language What are FILES in C Storage of information in variables/arrays or pointers is (temporary). The data is these variables is loss when we shut down or switch off our system. Files are used for permanent retention of data. (Never loss during shut down or power failure). Permanent retention can be Hard Disk, Flash Drives, Floppy Disks, CD ROMs etc. Notepad or Microsoft word are the major applications of FILING. Example (Permanent retention): We can read our data in Microsoft word or notepad which was written 2 months ago.

4 Lecture 26: FILING HANDLING in C Language Type of FILES Text type files –Text type files are those type of information/data, which are easily readable by humans, Example include (*.txt, *.doc, *.ppt, *.cpp, *.c, *.h). –We can read these types of FILES by opening in Notepad or any other word processing system. Binary type files –Binary type files can’t be readable or modify able by the humans. –Only a particular software can open or view these types of FILES. –Example include (*.gif, *.bmp, *.jpeg, *.exe, *.obj, *.dll).

5 Lecture 26: FILING HANDLING in C Language FILE accessing in Computer Science In Computer Science we can access the content of any FILE, by using two types –Sequential access. –Direct access (Random access). Sequential Access –Files contents are access sequentially (from first to desire content). –To access the location 101th, we must first traverse all contents from 0 to 100. Then the 101th location is access. –(Normally slow when we want to access random contents in file). Random access is especially access in Databases.

6 Lecture 26: FILING HANDLING in C Language Sequential Access FILE (Example) NIC# Candidate Name Age 1R. Agrawal32 2R. Srikant25 3C. Bettini23 4D. Burdick40 5R. Zaki21 6….26 7….35 To access the data of the candidate of NIC# 5. We have to follow the following 3 steps. 1.First open the file. 2.Must have to traverse all the contents of NIC# 1, 2, 3 and 4. Then finally we can access NIC# 5 data. 3.Close the file.

7 Lecture 26: FILING HANDLING in C Language FILE access in Computer Science Direct Access –In direct access, we can access any location data directly without traversing those contents which are stored before that location. –Very FAST than normal sequential accessing.

8 Lecture 26: FILING HANDLING in C Language Direct Accessing FILE (Example) Suppose, we want to access NIC# 5 data. In direct access, we will move directly to the location NIC # 5. NIC # Candidate Name Age 1R. Agrawal32 2R. Srikant25 3C. Bettini23 4D. Burdick40 5R. Zaki21 6R. Agrawal26 7R. Srikant35

9 Lecture 26: FILING HANDLING in C Language Opening a FILE Before we write or read a FILE from a disk, we must open it. Opening a FILE established a protocol (communicate) between our program and the FILE system. This protocol contains the following information. Which FILE we are going to access from which hard disk location. Example “c:\\program files\\mydata\\Database.txt”. 1.What is the MOD of accessing, read or write. 2.This communicate system is also called FILE descriptor.

10 Lecture 26: FILING HANDLING in C Language Opening a FILE Hard disk d:\\ Floppy disk z:\\ USB drive g:\\ CD/DVD ROM h:\\ FileAccess.exe Want to read file d:\\firstFile.txt Protocol (FILE descriptor) The main question is:- (1)How we can access this FILE descriptor is C language. (2)Every FILE descriptor is a pointer type information. We can store this pointer in a FILE type pointer.

11 Lecture 26: FILING HANDLING in C Language Files in C In C language, each FILE is simply a sequential stream of bytes. A FILE must first be opened properly before it accessed for reading or writing. file descriptorSuccessfully opening a FILE returns a pointer to (i.e., the address of) a file descriptor. … End of file marker 1 2 3 4 6 7 8 n-1 n

12 Lecture 26: FILING HANDLING in C Language FILE pointer in C The statement: FILE *fptr1, *fptr2 ; fptr1fptr2 FILE declares that fptr1 and fptr2 are pointer type variables of type FILE. They can contain the address of a file descriptors.

13 Lecture 26: FILING HANDLING in C Language Opening FILE The statement: FILE *fptr1; fptr1 = fopen( “d:\\mydata.txt", "r"); d:\\mydata.txt would open the file d:\\mydata.txt for input (reading). The statement: FILE *fptr2; fptr2 = fopen(“d:\\results.txt", "w"); d:\\results.txt would open the file d:\\results.txt for output (writing).

14 Lecture 26: FILING HANDLING in C Language Opening Text Files The statement: FILE *fptr2; fptr2 = fopen (“d:\\results.txt",“r+"); d:\\results.txt would open the file d:\\results.txt for both reading and writing. Once the FILE is open, it stay open until you close it or end of the program reaches (which will close all files.)

15 Lecture 26: FILING HANDLING in C Language Testing for Successful Open fopenIf the FILE was not able to be opened, then the value returned by the fopen routine is NULL. d:\\mydata.txtFor example, let's assume that the file d:\\mydata.txt does not exist. Then: FILE *fptr1; fptr1 = fopen ( “d:\\mydata.txt", “W") ; if (fptr1 == NULL) cout<< "File 'mydata' can’t be open”; else cout << “File opens successfully”;

16 Lecture 26: FILING HANDLING in C Language Writing in FILE We can write any contents (data) in the FILE using the following FILING function: fprintf (paramter1, paramter2, paramter3); FILE pointerSignature of the variable Actual name of the variable

17 Lecture 26: FILING HANDLING in C Language Writing in FILE (Example 1) fprintf The fprintf function will write value from the number1 (memory location) to hard disk location (“d:\\mydata.txt”). void main (void) { FILE *fptr; fptr = fopen( “d:\\mydata.txt”, ”w” ); // write mod int number1 = 93; fprintf (fptr,”%d”, number1); }

18 Lecture 26: FILING HANDLING in C Language Writing in FILE (Example 2) fprintf The fprintf function will write value from the number1 & floatData (memory location) to hard disk location (“d:\\myfile.dat”). void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat”, ”w” ); // write mod long number1 = 93; float floatData = 34.63; fprintf (fptr,”%d%f”, number1,floatData); }

19 Lecture 26: FILING HANDLING in C Language Writing in FILE (Example 3) fprintf The fprintf function will write value from the number1, floatData and myCharacter (memory location) to hard disk location (“d:\\mydata.txt”). void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”w”); // write mod long number1 = 93; float floatData = 34.63; char myCharacter = ‘D’; fprintf (fptr,”%d%f%c”, number1,floatData,myCharacter); }

20 Lecture 26: FILING HANDLING in C Language Writing in FILE (Example 4) fprintf The fprintf function will write value from the string 1 (memory location) to hard disk location (“d:\\mydata.txt”). void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat”, ”w” ); // write mod char string1 [40]; strcpy (string1, “My text information is Pakistan”); fprintf (fptr,”%s”, string1); }

21 Lecture 26: FILING HANDLING in C Language Writing in FILE (problem with “w” MOD) What would be the final data in the FILE name myfile.data when the above code executes 3 times. The final data would be only (353) not (353353353). The reason is that, with “w” MOD, on every run the previous data is REMOVED, and the new data is written from beginning of FILE. void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat”, ”w” ); // write mod int data = 353; fprintf (fptr,”%d”, data); }

22 Lecture 26: FILING HANDLING in C Language Solution Open in Append MOD “a+” or “w+” In append MOD, the previous data in the FILE is not removed on every new execution. The data in the FILE name “d:\\myfile.data” on the 3 runs would be now (353353353). void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”a+”); // write mod int data = 353; fprintf (fptr,”%d”, data); }

23 Lecture 26: FILING HANDLING in C Language Reading Assignment Read Pages 536 to 556 of “Robert Lafore” book “Turbo C Programming for the PC”.


Download ppt "Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26."

Similar presentations


Ads by Google