Introduction to file management

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

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

File IO and command line input CSE 2451

System Software Experiment 1 Lecture 7

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

Organization of a file

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

Input/Output and the Operating Systems

Standard File Pointers

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

PROGRAMMAZIONE I A.A. 2017/2018

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

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

C-Refresher: Session 10 Disk IO

File Handling. Reference:

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

M.CS201 Programming language

C programming basics T3-1 -

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

C Basics And Concepts Input And Output

COMP1917: 15 File IO

Introduction to C Recursion, sorting algorithms, files

CS240: Programming in C

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

Naked C Lecture 6. File Operations and System Calls

Standard C Library Functions

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

Input / Output Functions

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

Procedural Programming

EM108 Software Development for Engineers

ENG120. Misc. Topics

Basic and Practice in Programming Lab 10

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

UNIX System Programming

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

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

25.2 Opening and Closing a File

Intermediate Programming, Spring 2017*

CE Lecture 11

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

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

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

C mini reference. 5 Binary numbers 12

Computer System and programming in C

Data File and File Handling

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Fundamentals of Programming & Procedural Programming

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

2009 S2 COMP File Operations

Programming Fundamentals

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

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

Programming & Data Structure

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

Computer Programming Unit v

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

211: Computer Architecture Summer 2016

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

Chapter 10. File Processing 248 FILE PROCESSING

mywbut.com 12 File Input/Output

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

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

SAE1A Programming in C. Unit : I - V

Help Session 2. Programming Assignment 2

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

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

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

Engineering program development 7. Edited by Péter Vass

structs as arguments

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

Fundamentals of Programming. Lecture 15: C File Processing

Introduction to Computer Programming Lecture 18 Binary Files

Computer programming

CS1003: Intro to CS, Summer 2008

C File Processing: One-Page Summary

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

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

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

C Programming Language

Today s Learning Objectives

Pointers and File Handling

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

IO = Input & Output 2

Laboratory: USING FILES I. THEORETICAL ASPECTS

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

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

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)

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

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

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)

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke

Lecture 9: File Processing. Quazi Rahman

Text Output and Input; Redirection

Transcription:

1

Introduction to file management Some application require input to be taken from a file and output is required to be stored in a file. The C language provides the facility of file input-output operations. The standard library in C has many file I/O functions. File is a place on disk where a group of related data is stored. Sequence of steps for operating on a file: Naming or creating a file Opening a file Reading or writing to the file Closing the file 2

Opening a file While working with file, you need to declare a pointer of type file. This declaration is needed for communication between file and program. FILE *fp; -- declaration Opening a file is performed using library function fopen(). The syntax for opening a file in standard I/O is: fp = fopen ( filename, mode ); For example, fp = fopen("e:\\cprogram\\program.txt","w"); In the above statement, fp is a pointer to the data type FILE. The above statement opens a file named filename and assigns an identifier to the FILE type pointer fp. This pointer which contains all the information about the file, is subsequently used as a communication link between the system and the program. The function fopen returns a pointer to the opened file stream. The parameter filename is the name of the file to be opened. The mode string can have one of the values showed in the table. 3

Opening a file Mode string for fopen( ) Mode Description r w a Open file for reading only Open file for write purpose. Create for writing and if the file already exist, it will be overwritten Append, open for writing at end of file (create for writing if doesn t exist) r+ Open an existing file for update (reading or writing) w+ Create a new file for update. If it already exists, it will be overwritten a+ Open for append, open for update at the end of the file. 4

closing a file The file should be closed after reading/writing of a file. Closing a file is performed using library function fclose(). fclose(file_pointer); For example, fclose(fp); 5

Input/output operations on a file Function Name fopen( ) fgetc( ) fputc( ) fclose( ) fprintf( ) fscanf( ) fputs( ) fgets( ) fread( ) fwrite( ) Operation Open the file for use Read a character from the file Writes a character to the file Close a file, which is open by file pointer Write a set of data values to files Read a set of data values from files. Write string to file Read string from file Read records (sequence of bytes) to the file. Write records (sequence of bytes) to the file. 6

fgetc( ) and fputc( ) functions The function fgetc( ) and fputc( ) are used to perform character reading and writing from files. Assume that a file is opened with mode write and file pointer fp1. Following statement, fputc(c, fp1); Where fp1 is file pointer and c is character type variable, which write character c at fp1 position in the file. Similarly, the fgetc( ) reads one character at a time from the file opened in read mode and moves the file pointer to next position. c = fgetc(fp); Where c is a character type variable, fp is a file pointer. Refer the program fgetc.c, fputc.c & copy.c 7

fscanf( ) and fprintf( ) functions For handling group of different data types from file at a time, fscanf( ) and fprintf( ) function is used. The general format of the fscanf( ) is given below: fscanf(fp, control string, &arguments); Where fp is a file pointer associated with file that has been opened for reading. The control string contains output specifications for the items in the list. The list may include variables, constants and string. For example, in the following code, int a; char b; fscanf(fp, %d %c, &a, &b); Here, a and b are variables in which data is read from file into variables. 8

fscanf( ) and fprintf( ) functions The general format of the fprintf( ) is given below: fprintf(fp, control string, list); Where fp is a file pointer associated with file that has been opened for writing. The control string contains output specifications for the items in the list. The list may include variables, constants and string. For example, in the following code, fprintf(fp, %s %d, city, total); Here, city is an array variable of type char and total is an int variable. Refer the program fprintf.c and fscanf.c 9

fgets( ) and fputs( ) functions The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. Following is the declaration for fgets() function. char *fgets(char *str, int n, FILE *stream) Parameters str -- This is the pointer to anarray of chars where the string read is stored. n -- This is the maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used. stream -- This is the pointer to a FILE object that identifies the stream where characters are read from. Refer program fgets.c 10

fgets( ) and fputs( ) functions The C library function int fputs(const char *str, FILE *stream) writes a string to the specified stream up to but not including the null character. Following is the declaration for fputs() function. int fputs(const char *str, FILE *stream) Parameters str -- This is an array containing the null-terminated sequence of characters to be written. stream -- This is the pointer to a FILE object that identifies the stream where the string is to be written. Refer program fputs.c 11

fwrite( ) function The fwrite() function is used to write records (sequence of bytes) to the file. A record may be an array or a structure. Syntax of fwrite() function fwrite( ptr, int size, int n, FILE *fp ); The fwrite() function takes four arguments. ptr : ptr is the reference of an array or a structure stored in memory. size : size is the total number of bytes to be written. n : n is number of times a record will be written. FILE* : FILE* is a file where the records will be written in binary mode. 12

fread( ) function The fread() function is used to read bytes form the file. Syntax of fread() function fread( ptr, int size, int n, FILE *fp ); The fread() function takes four arguments. ptr : ptr is the reference of an array or a structure where data will be stored after reading. size : size is the total number of bytes to be read from file. n : n is number of times a record will be read. FILE* : FILE* is a file where the records will be read. 13

Command line arguments Command line arguments are parameter or arguments passed to a program when it runs from command prompt or invoked through turboc environment. Example: C:\tc\bin > file1 a.txt b.txt Where file1 is the program name where executable code of the program is stored, and a.txt is the first argument passed to the program and b.txt is the second argument passed to the program. These arguments are recognized into program by main( ) method. For handling command line arguments, main( ) function has to be written with two arguments are shown below: void main (int argc, char *argv[ ]) argc is argument count and argv array stores arguments passed to program. For example, argv [0] = file1, argv[1] = a.txt, argv[2] = b.txt The first parameter in the command line is always the program name and therefore argv[0] always represents the program name. Refer the program commandline.c 14

15