ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
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.
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.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Accessing Files in CCS-2303, C-Term Accessing Files in C CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
File Handling Spring 2013Programming and Data Structure1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
 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.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
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 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
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.
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:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 7 Text Input/Output Objectives
C Programming Files I/O
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
EKT120: Computer Programming
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
What you need for the 1st phase of project
Chapter 11 – File Processing
Lecture 13 Input/Output Files.
C Programming Lecture-15 File I/O
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Files Chapter 8.
Presentation transcript:

ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology (KUET) Lecturer

WHAT IS FILE Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)  Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image.  The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. It is conceivable (and it sometimes happens) that a graphics file will be read and displayed by a program designed to process textual data. The result is that no meaningful output occurs (probably) and this is to be expected. A file is simply a machine decipherable storage media where programs and data are stored for machine usage. Folder

TYPES OF FILE  Essentially there are two kinds of files that programmers deal with text files and binary files, 1. ASCII Text files  A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time. 2. Binary files  A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. Hence a binary file is also referred to as a character stream, but there are two essential differences.  No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed.  C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer. Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)

WHY FILES ARE NEEDED? Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)  When the program is terminated, the entire data is lost in C programming. If you want to keep large volume of data, it is time consuming to enter the entire data. But, if file is created, these information can be accessed using few commands.  There are large numbers of functions to handle file I/O in C language. In this tutorial, you will learn to handle standard I/O(High level file I/O functions) in C. File Operations  Creating a new file  Opening an existing file  Reading from and writing information to a file  Closing a file

WORKING WITH FILE While working with file, you need to declare a pointer of type file. This declaration is needed for communication between file and program.  FILE *ptr; Opening a file Opening a file is performed using library function fopen(). The syntax for opening a file in standard I/O is: ptr=fopen("fileopen","mode") For Example: fopen("E:\\cprogram\program.txt","w"); E:\\cprogram\program.txt is the location to create file. "w" represents the mode for writing. Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET) “W” is Mode

FILE MODES IN STANDARD I/O File Mode Meaning of ModeDuring Inexistence of file r Open for reading.If the file does not exist, fopen() returns NULL. w Open for writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created. a Open for append. i.e, Data is added to end of file. If the file does not exists, it will be created. r+ Open for both reading and writing.If the file does not exist, fopen() returns NULL. w+ Open for both reading and writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created. a+ Open for both reading and appending. If the file does not exists, it will be created. Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)

OTHER FILE OPERATIONS Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET) Closing a File The file should be closed after reading/writing of a file. Closing a file is performed using library function fclose(). fclose(ptr); //ptr is the file pointer associated with file to be closed.// The Functions fprintf() and fscanf() functions. The functions fprintf() and fscanf() are the file version of printf() and scanf(). The only difference while using fprintf() and fscanf() is that, the first argument is a pointer to the structure FILE

EXAMPLE: WRITING TO A FILE #include int main() { int n; FILE *fptr; fptr=fopen("C:\\program.txt","w"); if(fptr==NULL){ printf("Error!"); exit(1); } printf("Enter n: "); scanf("%d",&n); fprintf(fptr,"%d",n); fclose(fptr); return 0; } Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET) This program takes the number from user and stores in file. After you compile and run this program, you can see a text file program.txt created in C drive of your computer. When you open that file, you can see the integer you entered.

EXAMPLE: READING FROM FILE Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET) #include int main() { int n; FILE *fptr; if ((fptr=fopen("C:\\program.txt","r"))==NULL) { printf("Error! opening file"); exit(1); /* Program exits if file pointer returns NULL. */ } fscanf(fptr,"%d",&n); printf("Value of n=%d",n); fclose(fptr); return 0; } If you have run program above to write in file successfully, you can get the integer back entered in that program using this program. Other functions like fgetchar(), fputc() etc. can be used in similar way.

BINARY FILES Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)  Depending upon the way file is opened for processing, a file is classified into text file and binary file.  If a large amount of numerical data it to be stored, text mode will be insufficient. In such case binary file is used.  Working of binary files is similar to text files with few differences in opening modes, reading from file and writing to file.  Opening modes of binary files  Opening modes of binary files are rb, rb+, wb, wb+,ab and ab+. The only difference between opening modes of text and binary files is that, b is appended to indicate that, it is binary file.

READING AND WRITING OF A BINARY FILE Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files. Function fwrite() takes four arguments, address of data to be written in disk, size of data to be written in disk, number of such type of data and pointer to the file where you want to write. fwrite(address_data,size_data,numbers_data,pointer_to_file); Function fread() also take 4 arguments similar to fwrite() function as above. Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)

THANK YOU Department of Mechanical Engineering Khulna University of Engineering & Technology (KUET)