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.

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.
UNIT 15 File Processing.
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.
KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 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.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 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.
File IO and command line input CSE 2451 Rong Shi.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
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.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Files A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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 Sem2-09/10 DKT121: Fundamental of Computer Programming1 Files Operations.
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.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
11 C File Processing.
DKT121: Fundamental of Computer Programming
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
EKT120: Computer Programming
File I/O.
CS111 Computer Programming
File Input/Output.
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Text and Binary File Processing
Review & Lab assignments
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)
EPSII 59:006 Spring 2004.
FILE handeling.
Files Operations.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

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 Using a test editor to create data files

8.2 FUNDAMENTALS OF C DATA FILES a data file is a named collection of records, normally kept in external storage. A record is the collection of related data values in a file. A text file stores data as readable A binary file consists of nonreadable characters All records in a test file are terminated by an end- of-file marker placed by the operating system to mark the physical end of the file.

C Files and Streams The most commonly used functions are 1. The function fopen to open files, that is, to establish a stream between a program and a file 2. The functions fscanf, fgetc, fgets, and fread for input of data from files 3. The functions fprintf, fputc, fputs, and fwrite for output of data to a file 4. The function feof to check for the end-of-file marker during input The function fclose to close a file, or detach it from the program

Fscanf(student_file, “&d”, &test_score); The prefix f reminder that they all operate on files.

Declaring Files FILE *personnel_file; Declares personnel_file as a file establishes a stream between your program and the file personnel_file * declared as a pointer that is a variable contains the address of another variable or program entity in the main memory The keyword FILE in a file declaration must be all uppercase.

Naming Data Files Internal file name is the name that the C system uses to identify a file among others that a program might process. External file name is the name that the operating system uses to identify a file in a disk directory For example: A:\PROSONNEL.TXT

Before we can process a file, we must properly associate the external and internal names for a file so that whenever our C program references the file, the operating system can understand which external file it should access for input or output purposes.

InternalFileName= fopen(ExternalFileName, OpenMode); Used file open modes are 1.“r”, can read data from it 2.“w”, to write data to it 3.“a”, append data to the end of an already existing file 4.“r+”, opens a file for update(both an input and an output file) 5.“w+”, destroys the file if it already exists and opens a new file for update

Open mode is important 1. The OpenMode parameter in the fopen statement is required 2. “w” and “w+” are destructive; will destroy that file and begin creating a new file 3. “a” and “a+” are nondestructive. Can read data from a file that has been opened in “a+” 4. Open mode of input “r” the file must exist physically

Example 8.2 FILE *outdata; Outdata=fopen(“a:OUTPUT.TXT”, “w”); Establish outdata as an output stream between our program and the external file A:OUTPUT.TXT Example 8.3 Char in_file_name[15];

Example 8.3 char in_file_name[15]; … FILE *indata; … printf(“Enter external file name for input file: “); gets(in_file_name); … indata=fopen(in_file_name, “r”);

File Open Verification If an fopen function call returns a NULL pointer valus, then the call has failed. NULL is a pointer variables. Exit(-1); Will terminate the program execution and return the control to the operating system with a value of –1 Abnormal program termination

Example 8.4 #include … if((indata=fopen(file_name, “r”)) ==Null) { printf(“File open error on %s”, file_name); exit(-1); }

fprintf(stderr, “File open error on %s”, file_name); printing to stderr is faster than printing to stdout because the message appears on the screen without any delay or intermediate character buffering.

Closing Files and the fclose Function Fclose(InternalFileName); Cuts the connection between the program and the file. The file pointer no longer exists Fclose(indata); Checking for End-of-File and the feof Function To detect the end-of-file marker at the end of a data file

The feof function returns a nonzero integer(true) if the most recent input operation on the file InternalFileName has detected the thd-of-file marker; For example, While (! feof(student file)).