Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.

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.
© 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.
File I/O.
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 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.
File IO and command line input CSE 2451 Rong Shi.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
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:
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
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)
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.
Files A collection of related data treated as a unit. Two types Text
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
 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.
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.
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
EKT120: Computer Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Lecture 15 Files.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Access (7.5) CSE 2031 Fall February 2019.
Topics Introduction to File Input and Output
File Handling in C.
EPSII 59:006 Spring 2004.
FILE handeling.
Module 12 Input and Output
Chapter 11 Files chap8.
Files Chapter 8.
Presentation transcript:

Introduction to Programming Using C Files

2 Contents Files Working with files Sequential files Records

3 Files Arrays can store many strings but – They are limited by the amount of memory in the computer – When your program stops the data is lost Files offer an alternative – Data is stored on disk – Available space is much larger – Data is retained even when your program stops

4 Files A file is – A named area on a disk – Stored as a stream of bytes – Can be read and/or written by programs A file can contain – Text data – Binary data

5 Working with Files To access a file – A program uses the function fopen() to open a file which establishes a connection to the file – The program uses the function fprintf() and fscanf() to write to the file and read from the file – The program uses the close() function to write any remaining data to the file and sever the connection to the file

6 Working with Files All of the file functions are declared in – #include When you open a file, it returns a data structure which references the file This must be stored as a file pointer – FILE *fp; After the file is opened, this variable will be used to indicate which file you want to access

7 The fopen() Function fopen() is the function which opens a file – FILE *fopen(char filename[], char mode[]); The first parameter is the name of the file The second parameter is the mode – “r” to read from the file and fail if the file does not exist – “w” to write to a new file or to overwrite the data in an existing file – “a” to append to the end of an existing file or to create a new file and append onto it

8 The fopen() Function If fopen() is successful, it returns – A pointer to a data structure describing the open file If unsuccessful, it returns – The value 0, defined as NULL in stdio.h We normally compare the value returned to NULL to make sure that the file opened correctly before we proceed

9 Sequential Files The files we will be dealing with are called sequential since – Data is written to them as a continuous stream – We read from the beginning of the file – We write data at the end of the file There is another type of file called random access that lets us read or write at any position in the file

10 fprintf() and fscanf() These functions write to and read from files They work just like printf and scanf except – They have an extra first parameter which points to the file they should write to or read from – fprintf(FILE *fp, char format[], data…); – fscanf(FILE *fp, char format[], data…); * See file_io_demo.c

11 fclose() This function – Closes a file when we no longer need it – Ensures that all data is on disk before the file is closed – Frees the data structures which reference the file – Might unlock the file on some operating systems – Should be called before your program terminates

12 Other File Functions rewind(fp) – Rewinds a file to the beginning so you can read it again c = fgetc(fp) – Reads a single character or returns -1 (EOF) if at end of file – It is best to declare c to be an int so that it is easy to compare to EOF

13 Other File Functions fgets(char buf[], int max, FILE *fp) – Reads an entire line from a file, including the newline character fputc(char ch, FILE *fp) – Writes a single character to a file fputs(char str[], FILE *fp) – Writes a string to a file

14 Records and Fields There are 2 types of files we use – Report files Designed to be viewed by people or printed out – Data files Stores data to be read by the same program or another program

15 Data Files Data files often repeat the same information over and over Consider a customer file – Customer number, – Customer name – Customer address – Customer balance This information is repeated for every customer

16 Data Files All of the information on one customer is called – A record Each piece of information on one employee is called – A field – Eg. The customer name field

17 Data Files There is no set way to organize such file but one way would be – Each record is on a line by itself – Each field is terminated by a semi-colon This would make our file look like this 2345;Bob Jones;94 Oak St;234.67; 9876;Joan Smith; 32 Elm St.; ;Fran Fox;183 Pine Ave.;439.00

18 More Logical Operations There is one more logical operator – ! – Not – This simply reverses true to false and false to true Eg. – !(x < y) – Is the same as – x >= y