N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Files in C Rohit Khokher.
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.
Unit II : Pointers in C and file handling Objective : 1.To understand the basic operations with pointers. 2.To make the student understand the concept.
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.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
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.
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 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
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.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C Programming Lecture 12 : File Processing
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in 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.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
By C. Shing ITEC Dept Radford University
Lecture 12 CIS 208 Friday, March 3rd , 2005.
Chapter 4 File Processing
RECURSION Recursion is a process where a function calls itself. main()
Lecture 11 File input/output
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
File Handling in C.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
Introduction to Programming
Chapter 11 – File Processing
Lecture 15 Files.
CSC215 Lecture Input and Output.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
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 Handling in C.
Fundamental of Programming (C)
File Handling in C.
Chapter 12: File I/O.
FILE handeling.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C

N305: C Programming Copyright ©2006  Department of Computer & Information Science What is a File? A file is a collection of related data that a computers treats as a single unit.A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down.Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down. When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device. C uses a structure called FILE (defined in stdio.h ) to store the attributes of a file.C uses a structure called FILE (defined in stdio.h ) to store the attributes of a file.

N305: C Programming Copyright ©2006  Department of Computer & Information Science Steps in Processing a File 1. Create the stream via a pointer variable using the FILE structure: FILE *p; 2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file.

N305: C Programming Copyright ©2006  Department of Computer & Information Science The basic file operations are fopen - open a file- specify how its opened (read/write) and type (binary/text)fopen - open a file- specify how its opened (read/write) and type (binary/text) fclose - close an opened filefclose - close an opened file fread - read from a filefread - read from a file fwrite - write to a filefwrite - write to a file fseek/fsetpos - move a file pointer to somewhere in a file.fseek/fsetpos - move a file pointer to somewhere in a file. ftell/fgetpos - tell you where the file pointer is located.ftell/fgetpos - tell you where the file pointer is located.

N305: C Programming Copyright ©2006  Department of Computer & Information Science File Open Modes from Table 7-1 in Forouzan & Gilberg, p. 400

N305: C Programming Copyright ©2006  Department of Computer & Information Science More on File Open Modes from Figure 7-4 in Forouzan & Gilberg, p. 401

N305: C Programming Copyright ©2006  Department of Computer & Information Science Additionally, r+ - open for reading and writing, start at beginningr+ - open for reading and writing, start at beginning w+ - open for reading and writing (overwrite file)w+ - open for reading and writing (overwrite file) a+ - open for reading and writing (append if file exists)a+ - open for reading and writing (append if file exists)

N305: C Programming Copyright ©2006  Department of Computer & Information Science File Open The file open function ( fopen ) serves two purposes:The file open function ( fopen ) serves two purposes: –It makes the connection between the physical file and the stream. –It creates “a program file structure to store the information” C needs to process the file. Syntax: filepointer= fopen(“filename”, “mode”);Syntax: filepointer= fopen(“filename”, “mode”);

N305: C Programming Copyright ©2006  Department of Computer & Information Science More On fopen The file mode tells C how the program will use the file.The file mode tells C how the program will use the file. The filename indicates the system name and location for the file.The filename indicates the system name and location for the file. We assign the return value of fopen to our pointer variable:We assign the return value of fopen to our pointer variable: spData = fopen(“MYFILE.TXT”, “w”); spData = fopen(“A:\\MYFILE.TXT”, “w”);

N305: C Programming Copyright ©2006  Department of Computer & Information Science More On fopen from Figure 7-3 in Forouzan & Gilberg, p. 399

N305: C Programming Copyright ©2006  Department of Computer & Information Science Closing a File When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file.When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file. To close a file, we use fclose and the pointer variable: fclose(spData);To close a file, we use fclose and the pointer variable: fclose(spData);

N305: C Programming Copyright ©2006  Department of Computer & Information Science fprintf() Syntax: fprintf (fp,"string",variables); Example: int i = 12; float x = 2.356; char ch = 's'; FILE *fp; fp=fopen(“out.txt”,”w”); fprintf (fp, "%d %f %c", i, x, ch);

N305: C Programming Copyright ©2006  Department of Computer & Information Science fscanf() Syntax: fscanf (fp,"string",identifiers); Example: FILE *fp; Fp=fopen(“input.txt”,”r”); int i; fscanf (fp,“%d",i);

N305: C Programming Copyright ©2006  Department of Computer & Information Science getc() Syntax: identifier = getc (file pointer); Example: FILE *fp; fp=fopen(“input.txt”,”r”); char ch; ch = getc (fp);

N305: C Programming Copyright ©2006  Department of Computer & Information Science putc() write a single character to the output file, pointed to by fp. Example: FILE *fp; char ch; putc (ch,fp);

N305: C Programming Copyright ©2006  Department of Computer & Information Science End of File There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the fscanf function:There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the fscanf function: FILE *fptr1; int istatus ; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == feof(fptr1) ) { printf ("End-of-file encountered.\n”) ; }

N305: C Programming Copyright ©2006  Department of Computer & Information Science Reading and Writing Files #include #include int main ( ) { FILE *outfile, *infile ; FILE *outfile, *infile ; int b = 5, f ; int b = 5, f ; float a = 13.72, c = 6.68, e, g ; float a = 13.72, c = 6.68, e, g ; outfile = fopen ("testdata", "w") ; outfile = fopen ("testdata", "w") ; fprintf (outfile, “ %f %d %f ", a, b, c) ; fprintf (outfile, “ %f %d %f ", a, b, c) ; fclose (outfile) ; fclose (outfile) ; infile = fopen ("testdata", "r") ; infile = fopen ("testdata", "r") ; fscanf (infile,"%f %d %f", &e, &f, &g) ; fscanf (infile,"%f %d %f", &e, &f, &g) ; printf (“ %f %d %f \n ", a, b, c) ; printf (“ %f %d %f \n ", a, b, c) ; printf (“ %f %d %f \n ", e, f, g) ; printf (“ %f %d %f \n ", e, f, g) ;}

N305: C Programming Copyright ©2006  Department of Computer & Information Science Example #include #include #include<conio.h> void main() { char ch; FILE *fp; fp=fopen("out.txt","r");while(!feof(fp)){ch=getc(fp);printf("\n%c",ch);}getch(); }

N305: C Programming Copyright ©2006  Department of Computer & Information Science fread () Declaration: size_t fread(void *ptr, size_t size, size_t n, FILE *stream); size_t fread(void *ptr, size_t size, size_t n, FILE *stream); Remarks: Remarks: fread reads a specified number of equal-sized data items from an input stream into a block. ptr = Points to a block into which data is read ptr = Points to a block into which data is read size = Length of each item read, in bytes size = Length of each item read, in bytes n = Number of items read n = Number of items read stream = file pointer stream = file pointer

N305: C Programming Copyright ©2006  Department of Computer & Information Science Example Example: #include #include int main() { FILE *f; FILE *f; char buffer[11]; char buffer[11]; if (f = fopen("fred.txt", “r”)) if (f = fopen("fred.txt", “r”)){ fread(buffer, 1, 10, f); buffer[10] = 0; fclose(f); printf("first 10 characters of the file:\n%s\n", buffer); } } return 0; return 0;}

N305: C Programming Copyright ©2006  Department of Computer & Information Science fwrite() Declaration: size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream); size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream); Remarks: Remarks: fwrite appends a specified number of equal-sized data items to an output file. ptr = Pointer to any object; the data written begins at ptr ptr = Pointer to any object; the data written begins at ptr size = Length of each item of data size = Length of each item of data n =Number of data items to be appended n =Number of data items to be appended stream = file pointer stream = file pointer

N305: C Programming Copyright ©2006  Department of Computer & Information Science Example Example: #include #include int main() { char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w");fwrite(a,1,10,fs);fclose(fs); return 0; }

N305: C Programming Copyright ©2006  Department of Computer & Information Science fseek() This function sets the file position indicator for the stream pointed to by stream or you can say it seeks a specified place within a file and modify it. SEEK_SET Seeks from beginning of file SEEK_SET Seeks from beginning of file SEEK_CUR Seeks from current position SEEK_CUR Seeks from current position SEEK_END Seeks from end of file SEEK_END Seeks from end of fileExample: #include #include int main() { FILE * f; f = fopen("myfile.txt", "w"); fputs("Hello World", f); fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END fputs(" India", f); fclose(f); return 0; } { FILE * f; f = fopen("myfile.txt", "w"); fputs("Hello World", f); fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END fputs(" India", f); fclose(f); return 0; }

N305: C Programming Copyright ©2006  Department of Computer & Information Science ftell() offset = ftell( file pointer ); "ftell" returns the current position for input or output on the file "ftell" returns the current position for input or output on the file #include #include int main(void) { FILE *stream; FILE *stream; stream = fopen("MYFILE.TXT", "w"); stream = fopen("MYFILE.TXT", "w"); fprintf(stream, "This is a test"); fprintf(stream, "This is a test"); printf("The file pointer is at byte %ld\n", ftell(stream)); printf("The file pointer is at byte %ld\n", ftell(stream)); fclose(stream); fclose(stream); return 0; return 0;}

N305: C Programming Copyright ©2006  Department of Computer & Information Science THANK YOU