Programming & Data Structure

Similar documents
File Handling. 21 July 2009 Programming and Data Structure 1

Pointers and File Handling

Organization of a file

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

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

File IO and command line input CSE 2451

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

Standard File Pointers

C programming basics T3-1 -

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Input / Output Functions

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

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

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

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

CSC 270 Survey of Programming Languages. Input and Output

Input/Output and the Operating Systems

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

Input/Output: Advanced Concepts

2009 S2 COMP File Operations

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

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)

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

Contents. A Review of C language. Visual C Visual C++ 6.0

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

System Software Experiment 1 Lecture 7

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

8. Characters, Strings and Files

C-Refresher: Session 10 Disk IO

File I/O Lesson Outline

File I/O Lesson Outline

C File Processing: One-Page Summary

PROGRAMMAZIONE I A.A. 2017/2018

25.2 Opening and Closing a File

Introduction to file management

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

ENG120. Misc. Topics

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

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

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

Standard C Library Functions

Ch 11. C File Processing (review)

File I/O BJ Furman 23OCT2010

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

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

Chapter 11 File Processing

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

211: Computer Architecture Summer 2016

Lecture 9: File Processing. Quazi Rahman

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

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

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

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

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

CS240: Programming in C

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

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

Text Output and Input; Redirection

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Goals of this Lecture

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

Computer Programming Unit v

I/O Management! Goals of this Lecture!

Intermediate Programming, Spring 2017*

I/O Management! Goals of this Lecture!

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

CE Lecture 11

Fundamentals of Programming. Lecture 15: C File Processing

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

DATA STRUCTURES USING C

Simple Output and Input. see chapter 7

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

C Basics And Concepts Input And Output

C PROGRAMMING. Characters and Strings File Processing Exercise

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Administrative Stuff. More on Strings, File I/O. Last Time. Java API

More on Strings, File I/O. September 8, 2016

File I/O. Preprocessor Macros

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

Computer programming

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

Lecture 7: file I/O, more Unix

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

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

Lecture6 File Processing

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Engineering program development 7. Edited by Péter Vass

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

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

Computer Programming: Skills & Concepts (CP) Files in C

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

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Lecture 03 Bits, Bytes and Data Types

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Today s Learning Objectives

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Transcription:

File Handling Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013

File File Handling File R&W argc & argv (1) A file is a named collection of data, stored in secondary storage (typically). File Operations Open Read Write Close

File File Handling File R&W argc & argv (2) How is a file stored? Stored as sequence of bytes, logically contiguous (may not be physically contiguous on disk). The last byte of a file contains the end-of-file character (EOF), with ASCII code 1A (hex). While reading a text file, the EOF character can be checked to know the end.

File File Handling File R&W argc & argv (3) Types of files Text file contains ASCII codes only Binary file may contain non-ascii characters. Ex: Image, audio, video, executable, etc. To check the end of file here, the file size (also stored on disk) needs to be checked.

fopen File Handling File R&W argc & argv (1) In C, we use FILE * to represent a pointer to a file. The function fopen is used to open a file. It returns NULL when it is unable to open the file. FILE *fptr; char filename[] = "file2.dat"; fptr = fopen (filename,"w"); if (fptr == NULL) { printf ("ERROR IN FILE CREATION"); /* DO SOMETHING */ }

fopen File Handling File R&W argc & argv (2) fptr = fopen (filename,"w"); The second argument of fopen is the mode in which we open the file. There are three modes: "r" opens a file for reading. "w" creates a file for writing, and writes over all previous contents (so be careful!). "a" opens a file for appending, i.e., writing on the end of the file.

fopen File Handling File R&W argc & argv (3) We can add a "b" character to indicate that the file is a binary file: "rb", "wb", or "ab". Example fptr = fopen("myimage.bmp", "rb");

exit File Handling File R&W argc & argv (1) On error checking, we may need an emergency exit from a program. In main(), we can use return to stop. In functions, we can use exit() to do this. The function exit is defined in stdlib.h library. exit(-1); in a function is exactly the same as return -1; in main().

exit File Handling File R&W argc & argv (2) FILE *fptr; char filename[]= "file2.dat"; fptr = fopen (filename,"w"); if (fptr == NULL) { printf("error IN FILE CREATION"); exit(-1); }...

fprintf() File Handling File R&W argc & argv (1) fprintf() works just like printf() except that its first argument is a file pointer. int a=10, b=100; FILE *fptr; fptr = fopen ("file.dat","w"); if (fptr == NULL) { printf("error IN FILE CREATION"); exit(-1);} fprintf (fptr, "Hello World!\n"); fprintf (fptr, "%d %d", a, b);

fscanf() File Handling File R&W argc & argv (1) We also read data from a file using fscanf(). int a, b; FILE *fptr; fptr = fopen ("input.dat", "r"); if (fptr == NULL) { printf("error IN FILE CREATION"); exit(-1); } fscanf (fptr, "%d %d", &x, &y);...

fgets() File Handling File R&W argc & argv (1) Reading lines from a file using fgets() FILE *fptr; char line [1000]; fptr = fopen ("input.dat", "r"); if (fptr == NULL) { printf("error IN FILE CREATION"); exit(-1); } while (fgets(line,1000,fptr)!= NULL) printf ("Read line %s\n",line);

fgets() File Handling File R&W argc & argv (2) fgets() takes 3 arguments: a string, maximum number of characters to read, and a file pointer. It returns NULL if there is an error (e.g., EOF).

fclose() File Handling File R&W argc & argv (1) We can close a file using fclose() and the file pointer. FILE *fptr; char filename[]= "myfile.dat"; fptr = fopen (filename,"w"); if (fptr == NULL) { printf ("Cannot open file to write!\n"); exit(-1);} fprintf (fptr,"new file created!\n"); fclose (fptr);

File Handling File R&W argc & argv Special file streams (1) Three special file streams are defined in <stdio.h>: stdin reads input from the keyboard stdout send output to the screen stderr prints errors to an error device (usually also the screen)

File Handling File R&W argc & argv Special file streams (2) #include <stdio.h> main(){ int i; fprintf(stdout,"give value of i \n"); fscanf(stdin,"%d",&i); fprintf(stdout,"value of i=%d \n",i); fprintf(stderr,"no error:\nbut an example to show error message.\n"); }

File Handling File R&W argc & argv Special file streams (3) Give value of i 15 Value of i=15 No error: But an example to show error message.

File Handling File R&W argc & argv Special file streams (4) Input File & Output File Redirection One may redirect the standard input and standard output to other files (other than stdin and stdout). Usage: Suppose the executable file is a.out. $./a.out <in.dat >out.dat scanf() will read input data from the file in.dat, and printf() will print the result on the (newly created) file out.dat.

File Handling File R&W argc & argv Special file streams (5) $./a.out <in.dat >>out.dat scanf() will read input data from the file in.dat, and printf() will append the result at the end of the file out.dat.

File Handling File R&W argc & argv Character read & write (1) A character reading or writing is equivalent to reading or writing a byte. With stdin and stdout: int getchar(); int putchar(int c); With files: int fgetc(file *fp); int fputc(int c, FILE *fp);

File Handling File R&W argc & argv Character read & write (2) #include <stdio.h> main(){ int c; printf("type text and press return to see it again \n"); printf("for exiting press <CTRL D> \n"); while((c = getchar())!= EOF) putchar(c); }

File Handling File R&W argc & argv Command Line Arguments (1) A program can be executed by directly typing a command at the $ prompt. $./a.out in1.dat in2.dat out.dat The individual items specified are separated from one another by spaces. First item is the program name. Variables argc and argv keep track of the items specified in the command line.

File Handling File R&W argc & argv Command Line Arguments (2) Command line arguments are passed by specifying them under main(): int main (int argc, char *argv[]); argc denotes argument count. *argv[] denotes the array of strings as command line arguments, including the command itself. Example $./a.out s.dat d.dat Here, argc = 3, argv[0] = "./a.out", argv[1] = "s.dat", argv[2] = "d.dat".

File Handling File R&W argc & argv Command Line Arguments (3) #include <stdio.h> #include <string.h> int main(int argc,char *argv[]){ FILE *ifp, *ofp; int i, c; char src_file[100], dst_file[100]; if(argc!=3){ printf("usage:./a.out <src_file> <dst_file> \n"); exit(0); }

File Handling File R&W argc & argv Command Line Arguments (4) else{ strcpy(src_file, argv[1]); strcpy(dst_file, argv[2]); } if ((ifp = fopen(src_file,"r")) == NULL){ printf ("File does not exist.\n"); exit(0); }

File Handling File R&W argc & argv Command Line Arguments (5) if ((ofp = fopen(dst_file,"w")) == NULL){ printf ("File not created.\n"); exit(0); } while ((c = fgetc(ifp))!= EOF) fputc (c,ofp); } fclose(ifp); fclose(ofp);