C-Refresher: Session 10 Disk IO

Similar documents
25.2 Opening and Closing a File

Input/Output and the Operating Systems

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

CP2 Revision. theme: file access and unix programs

File Handling. Reference:

System Software Experiment 1 Lecture 7

File IO and command line input CSE 2451

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

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

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

PROGRAMMAZIONE I A.A. 2017/2018

CS240: Programming in C

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

Standard I/O in C, Computer System and programming in C

Intermediate Programming, Spring 2017*

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

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

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

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

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Introduction to file management

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Computer Programming Unit v

Computer programming

UNIX System Programming

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

File I/O. Preprocessor Macros

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

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

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

2009 S2 COMP File Operations

CSI 402 Lecture 2 (More on Files) 2 1 / 20

Data File and File Handling

Standard C Library Functions

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

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

Input / Output Functions

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

ENG120. Misc. Topics

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

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

EM108 Software Development for Engineers

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

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

Lecture 9: File Processing. Quazi Rahman

HIGH LEVEL FILE PROCESSING

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

M.CS201 Programming language

Standard File Pointers

C File Processing: One-Page Summary

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

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

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

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

File and Console I/O. CS449 Spring 2016

C-Refresher: Session 06 Pointers and Arrays

C PROGRAMMING Lecture 6. 1st semester

C Programming Language

Ch 11. C File Processing (review)

Chapter 11 File Processing

Programming & Data Structure

C PROGRAMMING. Characters and Strings File Processing Exercise

Week 9 Lecture 3. Binary Files. Week 9

structs as arguments

Organization of a file

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

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

Computer System and programming in C

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

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

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

UNIT- 2. Binary File

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

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

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

The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

Lab # 4. Files & Queues in C

SAE1A Programming in C. Unit : I - V

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

Exercise Session 3 Systems Programming and Computer Architecture

C Basics And Concepts Input And Output

C programming basics T3-1 -

Naked C Lecture 6. File Operations and System Calls

File and Console I/O. CS449 Fall 2017

Laboratory: USING FILES I. THEORETICAL ASPECTS

Pointers and File Handling

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

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

Engineering program development 7. Edited by Péter Vass

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

Fundamentals of Programming & Procedural Programming

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Transcription:

C-Refresher: Session 10 Disk IO Arif Butt Summer 2017 I am Thankful to my student Muhammad Zubair bcsf14m029@pucit.edu.pk for preparation of these slides in accordance with my video lectures at http://www.arifbutt.me/category/c-behind-the-curtain/

Today s Agenda Introduction to Program Files Opening/Closing a Disk File Reading from an Open File Random Access Binary Files Muhammad Arif Butt (PUCIT) 2

Introduction When a program starts its execution, there are three streams that are open at the start of the program i. stdin ii. stdout iii. stderr We know how to read/write from these streams using formatted and unformatted I/O To read a file residing on the hard disk, we need to open a new stream between our program and the file residing on the hard disk Muhammad Arif Butt (PUCIT) 3

Opening/Closing a Disk File To open a stream between our program and the file we use fopen() library call Syntax File *fopen(const char* path,const char* mode); It takes two arguments both are of type string In case of success, it returns a pointer of type FILE*, pointing to the file In case of failure, it returns a NULL pointer First argument is the file name with the absolute or relative path Second argument specifies the mode for opening the file Muhammad Arif Butt (PUCIT) 4

Text file Symbol Binary file Symbol Mode Description r rb Opens the file for reading with pointer at start of file Call fails if file doesn t exist r+ r+b Same as r but allows writing as well w wb Opens file for writing with write pointer at start If file doesn t exist, a new file is created w+ w+b Same as w but allows reading as well a ab Opens file for writing, with write pointer at the end If file doesn t exist, creates a new file a+ a+b Same as a but allows reading as well

Opening/Closing a Disk File(cont ) Return value On success: fopen() returns a file pointer that is used in all the subsequent calls, i.e. read, write and finally close the file This pointer points to a structure of type FILE FILE structure contains information about the file like location of buffer current file offset(cfo) Opening mode Flags like EOF(End Of File flag)its value can be checked by feof(fp) Muhammad Arif Butt (PUCIT) 6

Opening/Closing a Disk File(cont ) On Error: fopen() returns NULL A file opened must be closed after you have performed all the necessary operations on it Closing a file breaks the connection between the file on disk and the file pointer int fclose(file *stream); /*this function is used to close an opened file*/ It takes file pointer as the argument and closes that file When a program terminates, all the opened files are automatically closed But it is a good practice to close all the opened files Muhammad Arif Butt (PUCIT) 7

Reading from an Open File There are two categories of functions 1. Unformatted 2. Formatted Unformatted functions 1. Reading character by character and displaying on stdout till EOF fputc() and fgetc() are the functions used for this Syntax int fgetc(file* stream); It takes file pointer as argument and reads from that file Muhammad Arif Butt (PUCIT) 8

Reading from an Open File(cont ) fgetc() reads a character from the file given as argument and returns it as an unsigned character cast to an int int fputc(int c,file *stream); fputc() writes character c to the file given as the 2 nd argument e.g. fputc(c,stdout); //it writes c to stdout Let s see a program using fgetc() and fputc() Muhammad Arif Butt (PUCIT) 9

Reading from an Open File(cont ) /*The program reads from file character by character till EOF and displays it on stdout*/ #include<stdio.h> #include<stdlib.h> int main(int argc,char* argv[]){ if(argc!=2){ //checking if file name given or not printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); exit(1);} FILE* fp=fopen(argv[1],"r"); /*passing argv[1] for the file name, as user will pass file name in it, and opening the file in read only mode*/

Reading from an Open File(cont ) if(fp==null){//checking if the file has opened or not perror("fopen() failed\n");exit(1);} int c; while((c=fgetc(fp))!=eof) by character till EOF*/ /*reading from file character fputc(c,stdout); /*printing character by character on stdout, could also be done using putc(c);*/ fclose(fp); return 0;} //closing the file

Reading from an Open File(cont ) When you execute the program like./a.out p1.c //p1.c is the name of this program file It will print the contents of file p1.c on screen Muhammad Arif Butt (PUCIT) 12

Reading from an Open File(cont ) 2. Reading Line by Line and displaying on stdout till EOF fputs() and fgets() are used for writing and reading respectively Syntax fgets(char* s,int size,file* stream); It reads size number of characters from file given as 3 rd argument and stores it in string s If fgets() reaches end of line, it places \n character in s and then \0 and then ends reading int fputs(const char* s,file *stream); It places the string s on the file given as the 2 nd argument Muhammad Arif Butt (PUCIT) 13

Reading from an Open File(cont ) /*The program reads from the file line by line and displays the data read on the screen*/ #include<stdio.h> #include<stdlib.h> int main(int argc,char* argv[]){ if(argc!=2){//checking if file name given or not printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); } exit(1); FILE* fp=fopen(argv[1],"r"); /*passing argv[1] for the file name, as user will pass file name in it, and opening the file in read only mode*/

Reading from an Open File(cont ) if(fp==null){ //checking if the file has opened or not perror("fopen() failed\n");exit(1);} char buff[512]; while(fgets(buff,512,fp)!=null) /*reading from file line by line till EOF*/ fputs(buff,stdout); /*printing line by line on stdout, could also be done using puts(buff);*/ } fclose(fp); return 0; //closing the file

Reading from an Open File(cont ) When you execute the program like./a.out p1.c /*p1.c is the name of this program file*/ It will print the contents of file p1.c on screen Reading and writing character by character is far slow than reading and writing line by line Now that we have done reading using Unformatted functions, let s write a program that writes to a file Muhammad Arif Butt (PUCIT) 16

Reading from an Open File(cont ) /*The program takes a string from user and writes it to a file character by character*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char* argv[]){ if(argc!=2){ printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); exit(1);} FILE* fp=fopen(argv[1],"a");/*opening the file in append mode*/ if(fp==null){ perror("fopen() failed\n");exit(1);}

Reading from an Open File(cont ) /*taking input from the user*/ char name[20]; printf("enter you friends name: "); fgets(name,20,stdin); int len=strlen(name); name[len-1]='\0';/*replacing \n with NULL character*/ int i=0; /*writing character by character from index 0 till NULL character*/ while(name[i]!='\0') fputc(name[i++],fp); fputc('\n',fp); fclose(fp); return 0;} /*placing newline character at the end*/

Reading from an Open File(cont ) When you execute the program like./a.out newfile.txt /*newfile.txt is the name of this program file*/ It will take input from user and write that to file character by character Now let s write a program that takes input from user and writes it to the file line by line Muhammad Arif Butt (PUCIT) 19

Reading from an Open File(cont ) /*Program takes a string from user and writes it to a file line by line*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char* argv[]){ if(argc!=2){ printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); } exit(1); FILE* fp=fopen(argv[1],"a");//opening in append mode

Reading from an Open File(cont ) if(fp==null){ perror("fopen() failed\n");exit(1);} char name[20]; printf("enter you friends name: "); scanf("%[^\n]s",name); /*taking input using scanf(), it will automatically place \0 at the end*/ fputs(name,fp); fputc('\n',fp); printf("done..bye..bye..\n"); fclose(fp); return 0;} //writing the complete name

Reading from an Open File(cont ) When you execute the program like./a.out newfile.txt /*newfile.txt is the name of this program file*/ It will take input from user and write that to file line by line Remember that the file may contain integers or floating point numbers, but we have read them all as characters If we want to perform any operations on the values, we may have to manually convert them to the appropriate datatype Muhammad Arif Butt (PUCIT) 22

Reading from an Open File(cont ) Formatted Functions Formatted functions provide us with the ability that we do not have to convert from characters to integers or other datatypes manually, rather they do this for us The functions used here are fscanf(); //which is similar to scanf() with an additional initial argument and that is the file pointer from where we want to read fprintf(); //it is similar to printf() with an additional initial argument and that is the file pointer where we want to write Muhammad Arif Butt (PUCIT) 23

Reading from an Open File(cont ) Syntax int fscanf(file* stream, const char* format, ); int fprintf(file* stream,const char* format, ); Muhammad Arif Butt (PUCIT) 24

Reading from an Open File(cont ) /*The program reads from a file using formatted functions*/ #include<stdio.h> #include<stdlib.h> int main(int argc,char* argv[]){ if(argc!=2){ printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); } exit(1); FILE* fp=fopen(argv[1],"r"); if(fp==null){ perror("fopen() failed\n");exit(1);}

Reading from an Open File(cont ) int n; fscanf(fp,"%d,",&n);/*here integer will be read from fp and stored in n*/ while(feof(fp)==0){ fprintf(stdout,"%d ",n); fscanf(fp,"%d,",&n); /*comma after %d indicates that read till a comma too, integers, by default, are read until a space, tab or newline character occurs, here comma is specified because the numbers may be comma separated*/ } printf("\n"); fclose(fp); return 0;}

Reading from an Open File(cont ) For the file picture shown, output of the above program is: 1 2 3 4 5 6 7 8 9 10 You see that the numbers in the file are space, tab, comma and newline separated And here, they have been shown on a single line Muhammad Arif Butt (PUCIT) 27

Random Access Till now, we have read sequential access, i.e. we have read and written the file in a sequence as we cannot jump from one location to the other In random access, we can jump from one location to the other location in the file and then read/write there Some of the functions related to random access are 1. ftell() long ftell(file* stream); It will tell the current location of the file offset in the file whose pointer has been passed as argument Muhammad Arif Butt (PUCIT) 28

Random Access(cont ) 2. rewind() void rewind(file *stream); It will take the current file offset to the beginning of the file 3. fseek() int fseek(file *stream, long offset, int whence); First argument is the pointer to the file Second argument is the no. of bytes to jump Third argument is the location from where offset no. of bytes are to jump Muhammad Arif Butt (PUCIT) 29

Random Access(cont ) In simple words, it says that jump (offset+whence) no. of bytes in the file stream Some constants for whence are SEEK_SET i.e. from the start of the file e.g. fseek(fp,0,seek_set); //where fp is the file pointer This statement is just like rewind(fp); fseek(fp,50,seek_set); It says that jump 50 bytes from the start of the file SEEK_CUR i.e. the current position of the file offset Muhammad Arif Butt (PUCIT) 30

Random Access(cont ) fseek(fp,50,seek_cur); It says that jump 50 bytes ahead from the current position of the file offset SEEK_END i.e. the end of the file fseek(fp,50,seek_end); It says that jump 50 bytes ahead from the end of the file In case of reading, if we try to read after jumping 50 bytes, it will create error However, in case of writing, it will work OK Muhammad Arif Butt (PUCIT) 31

Random Access(cont ) When we jump n bytes ahead from the end, it will create a hole of n bytes in the file, which will be containing NULL However, this does not affect the size of the file If we copy this file to another, the new file will not be containing a hole in it and will have more size than the original file Muhammad Arif Butt (PUCIT) 32

Binary Files Binary files are used for reading files of custom formats i.e. the files other than the text format For example, a.out is a binary file You cannot read a.out using cat program because cat program has been written to read only text files readelf program can be used to read binary files Other examples of binary files include image and video files Muhammad Arif Butt (PUCIT) 33

Binary Files(cont ) Let s discuss a scenario is which we need to create a binary file For example, here is an image of a file The file contains records of different friends with their Record no., Name and City Now, for example, we want to change the city of Jamil from Lahore to Rawalpindi Muhammad Arif Butt (PUCIT) 34

Binary Files(cont ) Now as Lahore takes 6 characters and Rawalpindi takes 10 characters, so if we try to make changes in this file it will overwrite Rauf s data So, one solution is that we make new file, write in it all the record till Jamil as it is, then write Rawalpindi instead of Lahore and then write the remaining record After that delete the old file and renaming the new file with the previous file name Doing all this stuff is obviously not a good idea, as it is going to take a lot of time Muhammad Arif Butt (PUCIT) 35

Binary Files(cont ) A better solution to all this is, we use Binary files instead of text files In binary files, we will allocate fix space to each record Consider, for example, the following configuration Record Number Name City Total Bytes: 4 Bytes(int) 20 Bytes 30 Bytes 54 Now, in this configuration we can easily move to a record number For example, to move to record number 11, we will use the following statement Muhammad Arif Butt (PUCIT) 36

Binary Files(cont ) fseek(fp,11*54,seek_set); As each record requires 54 bytes and we are to move to the 11 th record so we are stepping 11*54 bytes ahead from the start The functions used for reading and writing to binary files are fread() and fwrite() Syntax size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); Here, first argument is the structure pointer, where fread() will store the record read from file Muhammad Arif Butt (PUCIT) 37

Binary Files(cont ) Second argument is the size of structure Third is the number of records to read Fourth is the FILE* pointer from where we will read the record size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE *stream); Here, arguments are just like the previous ones with the difference that here data read from the structure variable will be written to the file specified Let s write some program to understand use of fread() and fwrite() Muhammad Arif Butt (PUCIT) 38

Binary Files(cont ) /*The program reads from a binary file using fread() displays the data on screen*/ #include<stdio.h> #include<stdlib.h> struct Student{ int id; char name[20]; char address[30];}; int main(int argc,char* argv[]){ if(argc!=2){ printf("invalid number of arguments entered. Please enter filename to display its contents...\n"); exit(1); } struct Student s1; and

Binary Files(cont ) FILE* fp=fopen(argv[1],"rb"); if(fp==null){ perror("fopen() failed\n");exit(1);} /*fread() used to store 1 record from fp to s1*/ fread((struct Student*)&s1,sizeof(s1),1,fp); while(!feof(fp)){//read till EOF printf("student ID: %d\n",s1.id); printf("name: %s\n",s1.name); printf("address: %s\n",s1.address); fread((struct Student*)&s1,sizeof(s1),1,fp);} fclose(fp); return 0;}

Binary Files(cont ) /*The program takes a student s data from user, stores it in a Student structure and saves the result in a binary file*/ #include<stdio.h> #include<stdlib.h> struct Student{ }; int id; char name[20]; char address[30]; int main(int argc,char* argv[]){ if(argc!=2){ printf("invalid number of arguments entered. Please enter filename to display its contents...\n");

Binary Files(cont ) exit(1); } struct Student s1; FILE* fp=fopen(argv[1],"wb"); if(fp==null){ perror("fopen() failed\n");exit(1);} printf("roll Number: "); scanf("%d",&s1.id); getchar(); printf("name: "); scanf("%[^\n]s",s1.name); getchar();

Binary Files(cont ) printf("address: "); scanf("%[^\n]s",s1.address); fwrite((struct Student*)&s1,sizeof(s1),1,fp); printf("done..bye Bye...\n"); fclose(fp); return 0;}

Muhammad Arif Butt (PUCIT) 44