File I/O. Arash Rafiey. November 7, 2017

Similar documents
Introduction to file management

File IO and command line input CSE 2451

EM108 Software Development for Engineers

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

System Software Experiment 1 Lecture 7

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Standard File Pointers

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

C programming basics T3-1 -

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Organization of a file

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Input / Output Functions

Laboratory: USING FILES I. THEORETICAL ASPECTS

M.CS201 Programming language

PROGRAMMAZIONE I A.A. 2017/2018

Engineering program development 7. Edited by Péter Vass

Fundamentals of Programming & Procedural Programming

2009 S2 COMP File Operations

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

C Programming 1. File Access. Goutam Biswas. Lect 29

C-Refresher: Session 10 Disk IO

UNIX System Programming

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Standard C Library Functions

UNIT IV-2. The I/O library functions can be classified into two broad categories:

fopen() fclose() fgetc() fputc() fread() fwrite()

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Programming Fundamentals

File Handling. Reference:

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Input/Output and the Operating Systems

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

ENG120. Misc. Topics

File Access. FILE * fopen(const char *name, const char * mode);

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Computer Programming Unit v

CE Lecture 11

C File Processing: One-Page Summary

COMP1917: 15 File IO

Chapter 12. Files (reference: Deitel s chap 11) chap8

Course organization. Course introduction ( Week 1)

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Ch 11. C File Processing (review)

25.2 Opening and Closing a File

C Basics And Concepts Input And Output

Chapter 10. File Processing 248 FILE PROCESSING

File (1A) Young Won Lim 11/25/16

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Data File and File Handling

Procedural Programming

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CS240: Programming in C

Chapter 11 File Processing

Stream Model of I/O. Basic I/O in C

Computer System and programming in C

C Programming Language

CS1003: Intro to CS, Summer 2008

HIGH LEVEL FILE PROCESSING

Programming & Data Structure

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Today s Learning Objectives

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

SAE1A Programming in C. Unit : I - V

File I/O. Preprocessor Macros

Lecture 9: File Processing. Quazi Rahman

C mini reference. 5 Binary numbers 12

Naked C Lecture 6. File Operations and System Calls

Pointers and File Handling

Goals of this Lecture

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

System Programming. Standard Input/Output Library (Cont d)

Lab # 4. Files & Queues in C

Intermediate Programming / C and C++ CSCI 6610

Lecture6 File Processing

Input/Output: Advanced Concepts

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

IO = Input & Output 2

Chapter 8 File Processing

Solutions to Chapter 7

CSI 402 Lecture 2 Working with Files (Text and Binary)

Chapter 10: File Input / Output

The Design of C: A Rational Reconstruction: Part 2

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CSCI 171 Chapter Outlines

Introduction to Computer Programming Lecture 18 Binary Files

Transcription:

November 7, 2017

Files File is a place on disk where a group of related data is stored.

Files File is a place on disk where a group of related data is stored. C provides various functions to handle files on the storage devices.

Files File is a place on disk where a group of related data is stored. C provides various functions to handle files on the storage devices.

File Pointer File pointer is used to let the program keep track of the file being accessed.

File Pointer File pointer is used to let the program keep track of the file being accessed. Example: FILE *filepointer name;

File Pointer File pointer is used to let the program keep track of the file being accessed. Example: FILE *filepointer name; File pointer points to a structure that contains information about the file, such as:

File Pointer File pointer is used to let the program keep track of the file being accessed. Example: FILE *filepointer name; File pointer points to a structure that contains information about the file, such as: current character position in the buffer whether file is being read or written whether errors or end of file have occurred.

File Pointer File pointer is used to let the program keep track of the file being accessed. Example: FILE *filepointer name; File pointer points to a structure that contains information about the file, such as: current character position in the buffer whether file is being read or written whether errors or end of file have occurred. The header file <stdio.h> includes FILE structure declaration.

File Access Opening a file: fopen( ) function can be used to create a new file or to open an existing file.

File Access Opening a file: fopen( ) function can be used to create a new file or to open an existing file. Syntax: FILE *fopen( char * filename, char * mode );

File Access Opening a file: fopen( ) function can be used to create a new file or to open an existing file. Syntax: FILE *fopen( char * filename, char * mode ); It returns a pointer to structure FILE.

File Access Opening a file: fopen( ) function can be used to create a new file or to open an existing file. Syntax: FILE *fopen( char * filename, char * mode ); It returns a pointer to structure FILE. If the file is successfully opened then fopen()returns a pointer to file and if it is unable to open a file then it returns NULL.

File Access Opening a file: fopen( ) function can be used to create a new file or to open an existing file. Syntax: FILE *fopen( char * filename, char * mode ); It returns a pointer to structure FILE. If the file is successfully opened then fopen()returns a pointer to file and if it is unable to open a file then it returns NULL. Filename is a string that holds the name of the file on disk (including a path like /cs/course if necessary).

File Access Modes of fopen() can be the following values:

File Access Modes of fopen() can be the following values:

File Access Example: FILE *fp; fp = fopen( xyz.txt, r );

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode.

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode. Closing a file: After reading/writing of a file, the file should be closed.

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode. Closing a file: After reading/writing of a file, the file should be closed. Closing of a file is performed using library function fclose().

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode. Closing a file: After reading/writing of a file, the file should be closed. Closing of a file is performed using library function fclose(). Syntax: int fclose(file *file pointer);

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode. Closing a file: After reading/writing of a file, the file should be closed. Closing of a file is performed using library function fclose(). Syntax: int fclose(file *file pointer); fclose() returns zero on success and returns EOF (end of file) if there is an error in closing the file. EOF is a constant defined in the header file stdio.h.

File Access Example: FILE *fp; fp = fopen( xyz.txt, r ); //This will open the file xyz.txt in read mode. Closing a file: After reading/writing of a file, the file should be closed. Closing of a file is performed using library function fclose(). Syntax: int fclose(file *file pointer); fclose() returns zero on success and returns EOF (end of file) if there is an error in closing the file. EOF is a constant defined in the header file stdio.h. Example: fclose(fp);

Writing to a file Writing to a file: When a file is opened for writing, it is created if it does not already exist.

Writing to a file Writing to a file: When a file is opened for writing, it is created if it does not already exist. If the file already exists then its old contents are discarded.

Writing to a file Writing to a file: When a file is opened for writing, it is created if it does not already exist. If the file already exists then its old contents are discarded. The function fprintf can be used to write to a file. It is similar to printf except that it takes a file pointer as its first argument.

Writing to a file Writing to a file: When a file is opened for writing, it is created if it does not already exist. If the file already exists then its old contents are discarded. The function fprintf can be used to write to a file. It is similar to printf except that it takes a file pointer as its first argument. Syntax: int fprintf(file *fp,char *format,...);

Writing to a file Writing to a file: When a file is opened for writing, it is created if it does not already exist. If the file already exists then its old contents are discarded. The function fprintf can be used to write to a file. It is similar to printf except that it takes a file pointer as its first argument. Syntax: int fprintf(file *fp,char *format,...); Example: FILE *fp; fp = fopen( xyz.txt, w ); fprintf(fp, HELLO \n );

Writing to a file fputc() is used to write a single character at a time.

Writing to a file fputc() is used to write a single character at a time. Syntax: int fputc (char c, FILE *fp);

Writing to a file fputc() is used to write a single character at a time. Syntax: int fputc (char c, FILE *fp); It writes a character to the file and returns success, or EOF if an error occurs.

Writing to a file fputc() is used to write a single character at a time. Syntax: int fputc (char c, FILE *fp); It writes a character to the file and returns success, or EOF if an error occurs. Example: FILE *fp; char c; fp = fopen( xyz.txt, w ); for (c = A ; c <= Z ; c++) fputc ( c, fp );

Writing to a file The function fputs() writes a string to the file.

Writing to a file The function fputs() writes a string to the file. Syntax: int fputs (char *s, FILE *fp);

Writing to a file The function fputs() writes a string to the file. Syntax: int fputs (char *s, FILE *fp); It returns a non-negative value on success or EOF in case of any error.

Writing to a file The function fputs() writes a string to the file. Syntax: int fputs (char *s, FILE *fp); It returns a non-negative value on success or EOF in case of any error. Example: FILE *fp; char c[] = Hello ; fp = fopen( xyz.txt, w ); fputs ( c, fp );

Example: FILE *fp; int n = 2017; fp = fopen( xyz.txt, w ); fprintf(fp, %s %s %s %d, We, are, in, n);

Reading a file Reading a file: A file can be opened for reading, only if it already exist.

Reading a file Reading a file: A file can be opened for reading, only if it already exist. Reading a file that does not exist is an error.

Reading a file Reading a file: A file can be opened for reading, only if it already exist. Reading a file that does not exist is an error. fscanf can be used to read from a file. It is similar to scanf except that it takes a file pointer as its first argument.

Reading a file Reading a file: A file can be opened for reading, only if it already exist. Reading a file that does not exist is an error. fscanf can be used to read from a file. It is similar to scanf except that it takes a file pointer as its first argument. Syntax: int fscanf(file *fp,char *format,...);

Reading a file Reading a file: A file can be opened for reading, only if it already exist. Reading a file that does not exist is an error. fscanf can be used to read from a file. It is similar to scanf except that it takes a file pointer as its first argument. Syntax: int fscanf(file *fp,char *format,...); Example: FILE *fp; char buff[100]; fp = fopen( xyz.txt, r ); fscanf(fp, %s, buff);

Reading a file fgetc() is used to read a single character from the file.

Reading a file fgetc() is used to read a single character from the file. Syntax: char fgetc (FILE *fp);

Reading a file fgetc() is used to read a single character from the file. Syntax: char fgetc (FILE *fp); It returns the character that is read from the file, or EOF if an error occurs.

Reading a file fgetc() is used to read a single character from the file. Syntax: char fgetc (FILE *fp); It returns the character that is read from the file, or EOF if an error occurs. Example: FILE *fp; char c; fp = fopen( xyz.txt, r ); while(c!=eof) { c = fgetc(fp); printf( %c,c); }

Reading a file The function fgets() is used to read a line from the file.

Reading a file The function fgets() is used to read a line from the file. Syntax: char *fgets (char *s, int n, FILE *fp);

Reading a file The function fgets() is used to read a line from the file. Syntax: char *fgets (char *s, int n, FILE *fp); fgets reads a line until (n-1) characters are read or new line character is read, and stores it in the string pointed by s. It returns the parameter s on success or a null pointer in case of any error.

Reading a file The function fgets() is used to read a line from the file. Syntax: char *fgets (char *s, int n, FILE *fp); fgets reads a line until (n-1) characters are read or new line character is read, and stores it in the string pointed by s. It returns the parameter s on success or a null pointer in case of any error. Example: FILE *fp; char c[100]; fp = fopen( xyz.txt, r ); if( fgets(c,100,fp)!= NULL ) printf( %s,c);

Example: FILE *fp; int n; fp = fopen( xyz.txt, r ); fscanf(fp, %d, &n); // reading an integer n from xyz.txt fclose(fp);

Example Program to copy contents of one file to another (Using fgetc and fputc).

Example Program to copy contents of one file to another (Using fgetc and fputc). # include <stdio.h> void main() { FILE *fp1, *fp2; char a; fp1 = fopen( xyz.txt, r ); if ( fp1 == NULL ) { } printf( cannot open this file ); exit(1); fp2 = fopen( abc.txt, w );

Example if ( fp2 == NULL ) { printf( Not able to open this file ); fclose(fp1); exit(1); } do { a = fgetc(fp1); fputc(a, fp2); } while (a!= EOF); fcloseall(); }

Example Program to open a file and count the occurrence of the word system in the file.

Example Program to open a file and count the occurrence of the word system in the file. # include <stdio.h> # include <string.h> int main() { FILE *fp; char w[ ] = system ; char ch[100]; int i, count=0; fp = fopen( xyz.txt, r ); if ( fp == NULL ) { } printf( cannot open this file ); return(1);

Example while (!feof(fp) ) { //while end of file is not reached fscanf(fp, %s,ch); if( strcmp(w,ch)==0 ) count++; } printf( %s occurs %d times in the file \n,w,count); fclose(fp); return(0); }

fseek function char fseek ( FILE * stream, long int offset, int origin ) origin could be : SEEK SET : Beginning of file SEEK CUR : Current position of the file pointer SEEK END : End of file

fseek function char fseek ( FILE * stream, long int offset, int origin ) origin could be : SEEK SET : Beginning of file SEEK CUR : Current position of the file pointer SEEK END : End of file # include <stdio.h> int main () { FILE * pf; pf = fopen ( example.txt, w ); fputs ( This is a test, pf ); fseek ( pf, 10, SEEK SET ); fputs ( book, pf ); fclose (pf); return 0; }

Example Program to open a file and modify the third line to Hello World

Example Program to open a file and modify the third line to Hello World # include <stdio.h> # include <string.h> int main() { FILE *fp; int i, len, count=0; fp = fopen( xyz.txt, r ); if ( fp == NULL ) { printf( cannot open this file ); return(1); }

Example char Buffer[100], *buffpointer; while (!feof(fp) ) { //while! end of file and not three lines read } } getline(&buffer, &len, fp); buffpointer= Buffer; counter++; if (counter == 3) { fseek( fp, -strlen(buffpointer), SEEK CUR) ; fputs ( Hello World \n, fp); fclose(fp); break;

Changing a particular word in a file Write a program that reads a file and change all the words book to cook. # include <stdio.h> # include <string.h> int main() { FILE *fp; fp = fopen( example.txt, r+ ); if ( fp == NULL ) { } printf( cannot open this file ); return(1);

char oneword[100]; while (!feof(fp) ) { fscanf(fp, %s,oneword); if ( strcmp(oneword, book ) == 0 ) { } fseek( fp, -4, SEEK CUR) ; fputs ( cook, fp); } fclose(fp); }