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

Slides:



Advertisements
Similar presentations
Lecture 27: FILE HANDLING in C Language FILE HANDLING in C Language-2 Lecture 27.
Advertisements

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.
Files in C Rohit Khokher.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
C programming Language and Data Structure For DIT Students.
Program Input and the Software Design Process ROBERT REAVES.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Pascal Programming Files and Text (an intro). Pascal Programming Files... Data files need to be retained in secondary memory. The machine memory is random.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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.
Files Tutor: You will need ….
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
OUTLINE We will design a sample “Library” database in C language.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
Files A collection of related data treated as a unit. Two types Text
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
BASIC PROGRAMMING C SCP1103 (02)
MEMORY BYTES. MEMORY BYTES MEMORY MEMORY OUR Internal External.
User-Written Functions
BASIC PROGRAMMING C SCP1103 (02)
CS-103 COMPUTER PROGRAMMING
TMF1414 Introduction to Programming
CSCI 161: Introduction to Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Fundamental of Programming (C)
Machine Architecture and Number Systems
Files Chapter 8.
Presentation transcript:

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

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.

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.

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).

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.

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.

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.

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

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.

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.

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 n-1 n

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.

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).

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.)

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”;

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

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); }

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); }

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); }

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); }

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 ( ). 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); }

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 ( ). void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”a+”); // write mod int data = 353; fprintf (fptr,”%d”, data); }

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