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

Similar documents
Introduction to file management

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

File IO and command line input CSE 2451

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

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

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: 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

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

M.CS201 Programming language

Organization of a file

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

CS240: Programming in C

System Software Experiment 1 Lecture 7

Lecture 9: File Processing. Quazi Rahman

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

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

Standard C Library Functions

Input/Output and the Operating Systems

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

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

Standard File Pointers

Programming & Data Structure

C Basics And Concepts Input And Output

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

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

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

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Intermediate Programming, Spring 2017*

CSCI 171 Chapter Outlines

25.2 Opening and Closing a File

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

C programming basics T3-1 -

Ch 11. C File Processing (review)

Fundamentals of Programming & Procedural Programming

Pointers and File Handling

Engineering program development 7. Edited by Péter Vass

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

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

File Handling. 21 July 2009 Programming and Data Structure 1

File I/O. Preprocessor Macros

C-Refresher: Session 10 Disk IO

Computer System and programming in C

Input / Output Functions

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

ENG120. Misc. Topics

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Chapter 11 File Processing

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

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

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

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

CS240: Programming in C

C File Processing: One-Page Summary

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

Solutions to Chapter 7

EM108 Software Development for Engineers

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

C mini reference. 5 Binary numbers 12

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

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

Computer programming

Data File and File Handling

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.

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

CE Lecture 11

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

Naked C Lecture 6. File Operations and System Calls

Procedural Programming

Lecture6 File Processing

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

Computer Programming Unit v

UNIX System Programming

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

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

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

Lab # 4. Files & Queues in C

Fundamentals of Programming. Lecture 15: C File Processing

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

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

Input/Output: Advanced Concepts

SAE1A Programming in C. Unit : I - V

ECE264 Spring 2014 Exam 2, March 11, 2014

Programming. Functions and File I/O

C Programming Language

C: How to Program. Week /June/18

Week 9 Lecture 3. Binary Files. Week 9

CS1003: Intro to CS, Summer 2008

Text Output and Input; Redirection

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

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

Data Files. Computer Basics

Programming Fundamentals

SAMPLE MIDTERM SOLUTION

Transcription:

BIL 104E Introduction to Scientific and Engineering Computing Lecture 12

Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents a concrete device with which you want to exchange information. Before you perform any communication to a file, you have to open the file. Then you need to close the opened file after you finish exchanging information with it. The data flow you transfer from your program to a file, or vice versa, is called a stream, which is a series of bytes. Not like a file, a stream is device-independent. All streams have the same behavior. To perform I/O operations, you can read from or write to any type of files by simply associating a stream to the file. There are two formats of streams. The first one is called the text stream, which consists of a sequence of characters (that is, ASCII data). Depending on the compilers, each character line in a text stream may be terminated by a newline character. Text streams are used for textual data, which has a consistent appearance from one environment to another, or from one machine to another. The second format of streams is called the binary stream, which is a series of bytes. The content of an.exe file would be one example. Binary streams are primarily used for nontextual data, which is required to keep the exact contents of the file. 10/4/11 Lecture 12 2

I/O Statements Each file used in a program must have a file pointer associated with it. A file pointer is defined with a FILE declaration. FILE data type is defined in the header file stdio.h. After a file pointer is defined, it must be associated with a specific file. The fopen function obtains the information needed to assign a file pointer to a specific file. The two arguments for this function are the file name and a character that indicates the file status, which is also called the file open mode. If we are going to read information from a file with a program, the file open mode is r for read. If we are going to write information to a file with a program, the file open mode is w for write. The fclose function is used to close a file after we are finished with it. The function argument is the file pointer. 10/4/11 Lecture 12 3

File Opening Modes "r" opens an existing text file for reading. "w" creates a text file for writing. "a" opens an existing text file for appending. "r+" opens an existing text file for reading or writing. "w+" creates a text file for reading or writing. "a+" opens or creates a text file for appending. "rb" opens an existing binary file for reading. "wb" creates a binary file for writing. "ab" opens an existing binary file for appending. "r+b" opens an existing binary file for reading or writing. "w+b" creates a binary file for reading or writing. "a+b" opens or creates a binary file for appending. Note that you might see people use the mode "rb+" instead of "r+b". These two strings are equivalent. Similarly, "wb+" is the same as "w+b"; "ab+" is equivalent to "a+b". 10/4/11 Lecture 12 4

/* Opening and closing a file */ #include <stdio.h> enum {SUCCESS, FAIL; main(void) { FILE *fptr; char filename[]= "haiku.txt"; int reval = SUCCESS; if ((fptr = fopen(filename, "r")) == NULL){ printf("cannot open %s.\n", filename); reval = FAIL; else { printf("the value of fptr: 0x%p\n", fptr); printf("ready to close the file."); fclose(fptr); return reval; 10/4/11 Lecture 12 5

fgetc and fputc functions The fgetc and fputc functions are used to read/write one character at a time. The int fgetc(file *stream) function fetches the next character from the stream specified by stream. The function then returns the value of an int that is converted from the character. The int fputc(int c, FILE *stream) function prints the character that corresponds to the integer argument to the specified file stream. It then returns the same character as the function value if it is successful; otherwise, it returns EOF. After a character is written, fputc() advances the associated file pointer. 10/4/11 Lecture 12 6

/* Reading and writing one character at a time */ #include <stdio.h> enum {SUCCESS, FAIL; void CharReadWrite(FILE *fin, FILE *fout); main(void) { FILE *fptr1, *fptr2; char filename1[]= "outhaiku.txt"; char filename2[]= "haiku.txt"; int reval = SUCCESS; if ((fptr1 = fopen(filename1, "w")) == NULL) { printf("cannot open %s.\n", filename1); reval = FAIL; else if ((fptr2 = fopen(filename2, "r")) == NULL) { printf("cannot open %s.\n", filename2); reval = FAIL; else { CharReadWrite(fptr2, fptr1); fclose(fptr1); fclose(fptr2); return reval; void CharReadWrite(FILE *fin, FILE *fout) { int c; while ((c=fgetc(fin))!= EOF){ /* write to a file */ fputc(c, fout); /* put the character */ /*on the screen */ putchar(c); 10/4/11 Lecture 12 7

fgets Function The fgets function is used to read one line of text at a time. The char *fgets (char *s, int n, FILE *stream) function: Here s references a character array that is used to store characters read from the opened file pointed to by the file pointer stream. n specifies the maximum number of array elements. If it is successful, it returns the char pointer s. If an error occurs, the function returns a null pointer, and the contents of the array are unknown. The fgets() function can read up to n-1 characters, and can append a null character after the last character fetched, until a newline or an EOF is encountered. If a newline is encountered during the reading, the fgets() function includes the newline in the array. Note, this is different from what gets() function does. The gets() function just replaces the newline character with a null character. If EOF is encountered, the fgets() function returns a null pointer and leaves the array untouched. 10/4/11 Lecture 12 8

fputs Function The fputs function is used to write one line of text at a time. The int fputs (const char *s, FILE *stream) function: Here s points to the array that contains the characters to be written to a file associated with the file pointer stream. The const modifier indicates that the content of the array pointed to by s cannot be changed. If it fails, the fputs() function returns a nonzero value; otherwise, it returns zero. Note that the character array must include a null character at the end as the terminator to the fputs() function. Also, unlike the puts() function, the fputs() function does not insert a newline character to the string written to a file. 10/4/11 Lecture 12 9

/* Reading and writing one line of text at a time */ #include <stdio.h> enum {SUCCESS, FAIL, MAX_LEN = 81; void LineReadWrite (FILE *fin, FILE *fout); main(void) { FILE *fptr1, *fptr2; char filename1[]= "outhaiku.txt"; char filename2[]= "haiku.txt"; int reval = SUCCESS; if ((fptr1 = fopen (filename1, "w")) == NULL){ printf ("Cannot open %s for writing.\n", filename1); reval = FAIL; else if ((fptr2 = fopen (filename2, "r")) == NULL){ printf ("Cannot open %s for reading.\n", filename2); reval = FAIL; else { LineReadWrite(fptr2, fptr1); fclose (fptr1); fclose (fptr2); return reval; void LineReadWrite(FILE *fin, FILE *fout) { char buff[max_len]; while (fgets (buff, MAX_LEN, fin)!= NULL){ fputs (buff, fout); printf ("%s", buff); 10/4/11 Lecture 12 10

fread Function The fread function is used to read one block of characters at a time. The size_t fread (void *ptr, size_t size, size_t n, FILE *stream) function: Here ptr is a pointer to an array in which the data is stored. size indicates the size of each array element. n specifies the number of elements to read. stream is a file pointer that is associated with the opened file for reading. size_t is an integral type defined in the header file stdio.h. The fread() function returns the number of elements actually read. The number of elements read by the fread() function should be equal to the value specified by the third argument to the function, unless an error occurs or an EOF (end-of-file) is encountered. The fread() function returns the number of elements that are actually read, if an error occurs or an EOF is encountered. 10/4/11 Lecture 12 11

fwrite Function The fwrite function is used to write one block of characters at a time. The size_t fwrite (const void *ptr, size_t size, size_t n, FILE *stream) function: Here ptr references the array that contains the data to be written to an opened file pointed to by the file pointer stream. size indicates the size of each element in the array. n specifies the number of elements to be written. The fwrite() function returns the number of elements actually written. If there is no error occurring, the number returned by fwrite() should be the same as the third argument in the function. The return value may be less than the specified value if an error occurs. Note that it's the programmer's responsibility to ensure that the array is large enough to hold data for either the fread() function or the fwrite() function. 10/4/11 Lecture 12 12

feof Function The feof function can be used to determine when the end of a file is encountered. The int feof (FILE *stream) function Here stream is the file pointer that is associated with an opened file. The feof() function returns 0 if the end of the file has not been reached; otherwise, it returns a nonzero integer. This function is more useful when a binary file is read because the values of some bytes may be equal to the value of EOF. If the end of a binary file is determined by checking the value returned by fread(), we may end up at the wrong position. Using the feof() function helps us to avoid mistakes in determining the end of a file. 10/4/11 Lecture 12 13

/*Reading and writing one block at a time */ #include <stdio.h> enum {SUCCESS, FAIL, MAX_LEN = 80; void BlockReadWrite(FILE *fin, FILE *fout); int ErrorMsg(char *str); main(void) { FILE *fptr1, *fptr2; char filename1[]= "outhaiku.txt"; char filename2[]= "haiku.txt"; int reval = SUCCESS; if ((fptr1 = fopen(filename1, "w")) == NULL){ reval = ErrorMsg(filename1); else if ((fptr2 = fopen(filename2, "r")) == NULL){ reval = ErrorMsg(filename2); else { BlockReadWrite(fptr2, fptr1); return reval; fclose(fptr1); fclose(fptr2); void BlockReadWrite(FILE *fin, FILE *fout) { int num; char buff[max_len + 1]; while (!feof(fin)){ num = fread(buff, sizeof(char), MAX_LEN, fin); /* append a null character */ buff[num * sizeof(char)] = '\0'; printf("%s", buff); fwrite(buff, sizeof(char), num, fout); int ErrorMsg(char *str) { printf("cannot open %s.\n", str); return FAIL; 10/4/11 Lecture 12 14

fscanf function Once an input file and its pointer have been specified, information can be read from it by using the fscanf function. fscanf statement converts the characters from the lines in the data file to values. Example: Each line in the sensor1.dat file contains a time and sensor reading: #include <stdio.h> FILE *sensor1; sensor1 = fopen("sensor1.dat","r"); fscanf(sensor1, "%lf %lf", &time, &motion); fclose(sensor1); 10/4/11 Lecture 12 15

fprintf function If the file is an output file, we can write information to the file with the fprintf function. Example: #include <stdio.h> FILE *balloon; balloon = fopen("balloon.dat", "w"); fprintf (balloon, "%f %f %f\n", time, height, velocity/3600); fclose(balloon); 10/4/11 Lecture 12 16

/* This program generates a summary report from a data file */ /* that has the number of data points in the first record. */ #include <stdio.h> #incude <stdlib.h> #define FILENAME "sensor1.dat" main(){ int num_data_pts, k; double time, motion, sum=0, maximum, minimum; FILE *sensor1; sensor1 = fopen (FILENAME, "r"); fscanf (sensor1, "%i", &num_data_pts); for (k=1; k<=num_data_pts; k++);{ fscanf (sensor1, "%lf %lf", &time, &motion); if (k == 1) maximum = minimum = motion; sum += motion; if (motion > maximum) maximum = motion; if (motion < minimum) minimum = motion; printf ("Number of sensor readings: %i \n", num_data_pts); printf ("Average reading : %.2f \n", sum/num_data_pts); printf ("Maximum reading : %.2f \n", maximum); printf ("Minimum reading : %.2f \n", minimum); fclose (sensor1); return EXIT_SUCCESS; 10 0.0 132.5 0.1 147.2 0.2 148.3 0.3 157.3 0.4 163.2 0.5 158.2 0.6 169.3 0.7 148.2 0.8 137.6 0.9 135.9 10/4/11 Lecture 12 17

/* This program generates a summary report from a data file */ /* that has a trailer record with negative values*/ #include <stdio.h> #incude <stdlib.h> #define FILENAME "sensor2.dat" main(){ int num_data_pts=0, k; double time, motion, sum=0, maximum, minimum; FILE *sensor2; sensor2 = fopen (FILENAME, "r"); fscanf (sensor2, "%lf %lf", &time, &motion); maximum = minimum = motion; do{ sum += motion; if (motion > maximum) maximum = motion; if (motion < minimum) minimum = motion; num_data_pts++; fscanf(sensor2, "%lf %lf", &time, &motion); while (time >= 0); printf ("Number of sensor readings : %i \n", num_data_pts); printf ("Average reading : %.2f \n", sum/num_data_pts); printf ("Maximum reading : %.2f \n", maximum); printf ("Minimum reading : %.2f \n", minimum); fclose (sensor2); return EXIT_SUCCESS; 0.0 132.5 0.1 147.2 0.2 148.3 0.3 157.3 0.4 163.2 0.5 158.2 0.6 169.3 0.7 148.2 0.8 137.6 0.9 135.9-99 -99 10/4/11 Lecture 12 18

/* This program generates a summary report from a data file */ /* that does not have a header record or a trailer record */ #include <stdio.h> #incude <stdlib.h> #define FILENAME "sensor3.dat" 0.0 132.5 main(){ 0.1 147.2 int num_data_pts=0, k; 0.2 148.3 double time, motion, sum=0, maximum, minimum; 0.3 157.3 FILE *sensor3; 0.4 163.2 sensor3 = fopen (FILENAME, "r"); 0.5 158.2 while (( fscanf (sensor3, "%lf %lf", &time, &motion)) ==2){ 0.6 169.3 num_data_pts++; 0.7 148.2 if (num_data_pts ==1) maximum = minimum = motion; 0.8 137.6 sum += motion; 0.9 135.9 if (motion > maximum) maximum = motion; if (motion < minimum) minimum = motion; printf ("Number of sensor readings : %i \n", num_data_pts); printf ("Average reading : %.2f \n", sum/num_data_pts); printf ("Maximum reading : %.2f \n", maximum); printf ("Minimum reading : %.2f \n", minimum); fclose (sensor3); return EXIT_SUCCESS; 10/4/11 Lecture 12 19