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

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

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

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

Input/Output and the Operating Systems

System Software Experiment 1 Lecture 7

25.2 Opening and Closing a File

PROGRAMMAZIONE I A.A. 2017/2018

C File Processing: One-Page Summary

C-Refresher: Session 10 Disk IO

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

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

UNIX System Programming

File IO and command line input CSE 2451

CS240: Programming in C

EM108 Software Development for Engineers

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

M.CS201 Programming language

2009 S2 COMP File Operations

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

Data File and File Handling

Intermediate Programming, Spring 2017*

ENG120. Misc. Topics

File Handling. Reference:

File I/O. Preprocessor Macros

Introduction to file management

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

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

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

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

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

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

Lecture 9: File Processing. Quazi Rahman

Computer programming

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

Standard File Pointers

Ch 11. C File Processing (review)

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

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

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

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

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

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

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

Chapter 11 File Processing

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

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

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

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

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

Introduction to Computer Programming Lecture 18 Binary Files

HIGH LEVEL FILE PROCESSING

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

Input / Output Functions

Computer System and programming in C

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

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

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

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

File and Console I/O. CS449 Spring 2016

Computer Programming Unit v

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

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

Data Files. Computer Basics

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

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

C PROGRAMMING. Characters and Strings File Processing Exercise

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Lecture6 File Processing

C Basics And Concepts Input And Output

C Programming Language

UNIT- 2. Binary File

Engineering program development 7. Edited by Péter Vass

CP2 Revision. theme: file access and unix programs

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

File and Console I/O. CS449 Fall 2017

structs as arguments

Week 9 Lecture 3. Binary Files. Week 9

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

Standard C Library Functions

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

File I/O Lesson Outline

Programming & Data Structure

Organization of a file

Laboratory: USING FILES I. THEORETICAL ASPECTS

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

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

C: How to Program. Week /June/18

Basic and Practice in Programming Lab 10

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Chapter 8 File Processing

C PROGRAMMING Lecture 6. 1st semester

CS240: Programming in C

Transcription:

CMPE-013/L Outline File Processing File I/O Gabriel Hugh Elkaim Winter 2014 Files and Streams Open and Close Files Read and Write Sequential Files Read and Write Random Access Files Read and Write Random Access Files with Structures Definition File Processing FILE is just a data stream like any other, it is defined by a typedef in STDIO and is a pointer to a file that has several bits of information specific to the file that is in use (read/write access, buffer, current location within the file). Files and Streams C files C views each file simply as a sequential stream of bytes as shown below. It ends as if there is an end-of-file marker. Nothing special about FILE Just another pointer You ve already been using STDIN and STDOUT implicitly C opens three files on program startup STDIN (keyboard input) STDOUT (screen) STDERR (screen) A file with n bytes. 0 1 2... n-1 EOF 1

Stream Provide communication channel between files and programs Stream created when a file is opened Function feof() tests a stream for end-of-file. int feof(file *stream); returns nonzero if and only if the end-of-file indicator is set for stream. Open and Close Files A file is the most common I/O facility that can be used as a stream. Data type FILE, defined in stdio.h, maintains information about the stream. An object of type FILE *, created by calling a function such as fopen(), is used to access the file by other file manipulation functions such as fprintf() and fscanf(). Function fopen() is prototyped as FILE *fopen(const char *filename, const char *mode); filename is the name of the file which will be opened and associated with a stream. Argument modespecifies the meaning of opening a file. The valid values for this argument are described in the following table. r w The valid values for mode argument. Mode Meaning Open a text file for reading. Truncate to zero length or create a text file for writing. a Append; open or create a text file for writing at the end-of-file. rb wb ab Open a binary file for reading. Truncate to zero length or create a binary file for writing. Append; open or create a binary file for writing at the end-of-file. r+ Open a text file for read/write. w+ Truncate to zero length or create a text file for read/write. a+ Append; open or create a text file for read/write. You can read data anywhere in the file, but you can write data only at the end-of-file. r+b or rb+ Open a binary file for read/write. w+b or wb+ Truncate to zero length or create a binary file for read/write. a+b or ab+ Open and Close Files Append; open or create a binary file for read/write. You can read data anywhere in the file, but you can write only at the end-of-file. Open and Close Files If file cannot be opened, function fopen() returns NULL. All files which are opened and associated with streams should be closed before a program terminates. Function fclose()shall be used to close a file opened by function fopen(). It is prototyped as int fclose(file *stream); fclose()causes the stream pointed to by streamto be flushed and the associated file to be closed. It returns 0 when successful. Otherwise, it returns non-zero value. 2

Example: #include <stdio.h> FILE *fpt1, *fpt2; /* create file named filename" */ if((fpt1 = fopen( filename", w )) == NULL) { printf("cannot create or open the file\n"); /* open file filename for reading, starting at the beginning. */ if((fpt2 = fopen( filename, r )) == NULL) { printf("cannot open the file\n");... fclose(fpt1); fclose(fpt2); STDIN, STDOUT, STDERR Three special files standard input, standard output, and standard error. Above three files and their associated streams are automatically opened when the program execution starts and closed when the program terminates. The file pointers for these three streams are as below. stdin- standard input (keyboard) stdout- standard output (screen) stderr- standard error (screen) The standard input stream enables a program to read data from the keyboard, and standard output stream enables a program to print data on the screen. By default, the standard error stream is directed to the screen. Read and Write Charaters fgetc-reads one character from a file int fgetc(file *stream); fgetc(stdin) equivalent to getchar() fputc-writes one character to a file int fputc(int c, FILE *stream); fputc('a', stdout)equivalent to putchar('a') Copy a file character by character /* File: copyfile.chf for copying a file char by char */ #include <stdio.h> int copyfile(const char *inputfile, const char *outputfile) { FILE *fp1, *fp2; char c; if((fp1 = fopen(inputfile, "rb")) == NULL) return -1; if((fp2 = fopen(outputfile, "wb")) == NULL) { fclose(fp1); return -1; c = fgetc(fp1); while(!feof(fp1)) { fputc(c, fp2); c = fgetc(fp1); fclose(fp1); fclose(fp2); 3

Read and Write Lines fgets-read a line from a file char *fgets(char *s, int n, FILE *stream); fputs-write a line to a file int fputs(const char *s, FILE *stream); Copy a file line by line /* File: copyfile2.chf for copying a file line by line */ #include <stdio.h> #define BUFSIZE 1024 int copyfile2(const char *inputfile, const char *outputfile) { FILE *fp1, *fp2; char line[bufsize]; if((fp1 = fopen(inputfile, "rb")) == NULL) return -1; if((fp2 = fopen(outputfile, "wb")) == NULL) { fclose(fp1); return -1; fgets(line, BUFSIZE, fp1); while(!feof(fp1)) { fputs(line, fp2); fgets(line, BUFSIZE, fp1); fclose(fp1); fclose(fp2); Formatted Read and Write to Files fscanf/fprintf-file processing equivalents of scanfand printf int fprintf(file *stream, const char *format,...); int fscanf(file *stream, const char *format,...); Function calls printf(format, arglist); scanf(format, arglist); are equivalent to fprintf(stdout, format, arglist); fscanf(stdin, format, arglist); /* File: accelo.c Write accelerations into file accel.txt */ #include <stdio.h> /* for fopen(), fclose(), fprintf(), printf() */ #include <stdlib.h> /* for exit() */ #define M_G 9.81 /* gravitational acceleration constant g */ int main() { /* declaration and initialization */ double a, mu=0.2, m=5.0, t0=0.0, tf=10.0, step=1.0, p, t; int i, n; FILE *stream; /* a file stream */ Example Program /* open file accel.txt for writing */ stream = fopen("accel.txt", "w"); if(stream == NULL) { printf("error: cannot open 'accel.txt'\n"); exit(exit_failure); n = (tf - t0)/step + 1; /* number of points, 11 */ for(i=0; i<n; i++) { t = t0 + i*step; /* calculate value t */ p = 4*sin(t-3)+20; /* use t to calculate p */ a = (p-mu*m*m_g)/m; /* calculate a */ fprintf(stream, "%f\n", a);/* write acceleration into the file */ fclose(stream); /* close the file stream */ printf("done! Finished writing data to file accel.txt\n"); 4

Contents of file accel.txt: -0.362000 0.438000 1.238000 2.038000 2.838000 3.638000 4.438000 5.238000 6.038000 6.838000 7.638000 Problem Statement Write a program to read the accelerations from file accel.txt and print them out. Program : /* File: acceli.c Read accelerations from file accel.txt */ #include <stdio.h> /* for fopen(), fclose(), fscanf(), printf() */ #include <stdlib.h> /* for exit() */ int main() { double a; /* acceleration */ FILE *stream; /* a file stream */ /* open file accel.txt for reading */ stream = fopen("accel.txt", "r"); if(stream == NULL) { printf("error: cannot open 'accel.txt'\n"); exit(exit_failure); fscanf(stream, "%lf", &a); /* read an acceleration */ while(!feof(stream)) { /* while not end-of-file */ printf("%f\n", a); /* print the acceleration */ fscanf(stream, "%lf", &a); /* read the next acceleration */ fclose(stream); /* close the file stream */ Output: -0.362000 0.438000 1.238000 2.038000 2.838000 3.638000 4.438000 5.238000 6.038000 6.838000 7.638000 5

Problem Statement: For the GPA library, student information is contained in an application program. If the information of identification number, name, birth day, birth month, birth year, and GPA for a student is kept in a file rwstudent.data shown below 101 John 12 11 1985 3.33 read this information into a program and save the information in a new file rwstudentnew.data. /* File: rwstudent.c Read student information from file rwstudent.data and write it into another file "rwstudentnew.data" */ #include <stdio.h> /* for fopen(), fclose(), fprintf(), fscanf(), printf() */ struct Birthday { /* define structure for birthday */ short day, month, year; ; typedef struct Student { /* define structure for student */ int id; char name[32]; struct Birthday birthday; double gpa; student_t; /* read student information in filename into the buffer pointed by sp */ int readdata(student_t *sp, const char *filename) { FILE *stream; stream = fopen(filename, "r"); if(stream == NULL) { fprintf(stderr, "Error: cannot open file '%s' for reading\n", filename); return -1; fscanf(stream, "%d%s%hd%hd%hd%lf", &sp->id, sp->name, &sp->birthday.day, &sp->birthday.month, &sp->birthday.year, &sp->gpa); fclose(stream); /* write student information in the buffer pointed by sp into filename */ int writedata(const student_t *sp, const char *filename) { FILE *stream; stream = fopen(filename, "w"); if(stream == NULL) { fprintf(stderr, "Error: cannot open file '%s' for writing\n", filename); return -1; fprintf(stream, "%d %s %hd %hd %hd %g\n", sp->id, sp->name, sp->birthday.day, sp->birthday.month, sp->birthday.year, sp->gpa); fclose(stream); int main() { const char *rfilename = "rwstudent.data"; /* read this data file */ const char *wfilename = "rwstudentnew.data"; /* write into ths file */ student_t s; /* declare a student_t to hold the information */ int status; /* status for writing */ readdata(&s, rfilename); /* read data in rfilename into s */ status = writedata(&s, wfilename); /* write data from s into wfilename */ /* demonstrate how a return value should be used, give a message in stdout */ if(status) printf("writedata() failed\n"); else printf("writedata() is successful\n"); 6

Read and Write Random Access Files Random access files Access individual records without searching through other records Instant access to records in a file Data can be inserted without destroying other data Data previously stored can be updated or deleted without overwriting. Random Access File If a file supports random access, a file position indicator can be used to determine the position to read or write the next item. By default, the file position indicator points to the beginning of a file when it is opened. The reading or writing functions fread()and fwrite()will read or write items from the position pointed by the file position indicator and then increment the indicator accordingly. The function fseek()can be used to set the indicator to anywhere in the files supporting random access. Implemented using fixed length records Sequential files do not have fixed length records fwrite Binary Writing Write a specified number of bytes from a location in memory to a file. Function prototype size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream); ptr-location of written bytes from size-number of bytes for each item nitems-number of items to write stream-file to write Can write several array elements Provide pointer to array as first argument (ptr) Indicate number of elements to write as third argument (nitems) The return type size_tis defined in header file stdio.h. fread Binary Reading Reads a specified number of bytes from a file into memory Function prototype size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); ptr-location of read bytes to size-number of bytes for each item nitems-number of items to read stream-file to read Can read several fixed-size array elements Provide pointer to array (ptr) Indicate number of elements to read (nitems) 7

Moving within a FILE Moving within a FILE fseek Sets file position indicator to a specific position Fucntion prototype int fseek(file *stream, long int offset, int whence); stream --pointer to file offset --number of bytes from location whencein the file pointed to by stream. whence--has one of following three values SEEK_SET -- seek starts at beginning of file SEEK_CUR -- seek starts at current location in file SEEK_END --seek starts at end of file This function sets the file position indicator for the stream pointed to by argument stream. For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offsetto the position specified by whence. For a text stream, offsetshould be either zero or a value returned by an earlier successful call to function ftell()on a stream associated with the same file, and whence should be SEEK_SET. Function ftell()obtains the current value of the file position indicator for the stream. Read and Write Random Access Files With Structures Read and Write Random Access Files With Structures A structure is a data object. The values of a structure can be written to a file or read from a file by function fwrite() and fread(). Function fwrite() and fread() are used to write and read a block of data. The prototype for fwrite() and fread() are size_t fwrite(void *ptr, size_t size, size_t nitems, FILE *stream); size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); In order to write the data of a structure to a file, the first argument can be specified by the address of a structure and the second argument is the size of the structure. It can be calculated by the sizeof() operator. The program below illustrates how to use function fwrite() and fread() to write and read the values of a structure to or from a file. ptr A pointer to an memory location. For function fwrite(), the data written to a file are read from this location. For fread(), the data read from a file will be stored in this location. size-number of bytes for each item nitems-number of items to write or read stream-file pointer 8

/* File: fwrites.c */ #include <stdio.h> #include <stdlib.h> Output: s.id = 101 s.name = John struct Student { int id; char name[32]; ; int main() { struct Student s1 = {101, "John", s2; FILE *stream; stream = fopen("student.bin", "wb"); if(stream == NULL) { printf("error: cannot open 'student.bin' for writing.\n"); exit(exit_failure); fwrite(&s1, sizeof(struct Student), 1, stream); fclose(stream); Questions? stream = fopen("student.bin", "rb"); if(stream == NULL) { printf("error: cannot open 'student.bin' for reading.\n"); exit(exit_failure); fread(&s2, sizeof(struct Student), 1, stream); fclose(stream); printf("s2.id = %d\n", s2.id); printf("s2.name = %s\n", s2.name); remove("student.bin"); 9

10

11

12

13