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

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

File IO and command line input CSE 2451

Content. Input Output Devices File access Function of File I/O Redirection 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;

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

Standard File Pointers

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

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

System Software Experiment 1 Lecture 7

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

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

Introduction to file management

Input/Output and the Operating Systems

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

25.2 Opening and Closing a File

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

C-Refresher: Session 10 Disk IO

Input / Output Functions

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

M.CS201 Programming language

C File Processing: One-Page Summary

Engineering program development 7. Edited by Péter Vass

EM108 Software Development for Engineers

Programming & Data Structure

PROGRAMMAZIONE I A.A. 2017/2018

ENG120. Misc. Topics

HIGH LEVEL FILE PROCESSING

Fundamentals of Programming. Lecture 10 Hamed Rasifard

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Organization of a file

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

Data File and File Handling

Laboratory: USING FILES I. THEORETICAL ASPECTS

Fundamentals of Programming. Lecture 15: C File Processing

File I/O. Preprocessor Macros

CE Lecture 11

UNIX System Programming

C programming basics T3-1 -

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

Basic and Practice in Programming Lab 10

Fundamentals of Programming & Procedural Programming

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

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

CS240: Programming in C

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

Lecture 9: File Processing. Quazi Rahman

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

C Basics And Concepts Input And Output

2009 S2 COMP File Operations

Programming Fundamentals

Standard C Library Functions

Programming. Functions and File I/O

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)

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

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

Computer Programming Unit v

File Handling in C. EECS 2031 Fall October 27, 2014

Stream Model of I/O. Basic I/O in C

Ch 11. C File Processing (review)

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

Procedural Programming

SAE1A Programming in C. Unit : I - V

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

Chapter 8 File Processing

File Handling. 21 July 2009 Programming and Data Structure 1

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

File I/O BJ Furman 23OCT2010

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

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

File and Console I/O. CS449 Spring 2016

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Goals of this Lecture

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

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

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

Computer programming

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

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

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

COMP1917: 15 File IO

C Programming Language

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

CP2 Revision. theme: file access and unix programs

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

File and Console I/O. CS449 Fall 2017

Text Output and Input; Redirection

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

Chapter 11 File Processing

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

Simple Output and Input. see chapter 7

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

File I/O Lesson Outline

IO = Input & Output 2

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: Skills & Concepts (CP) Files in C

File I/O Lesson Outline

Transcription:

Chapter 14: File Processing

Files and Streams C views each file simply as a sequential stream of bytes. It ends as if there is an end-of-file marker. The data structure FILE, defined in stdio.h, stores the information needed to access the file/stream. Each OS has its own way to represent the EOF 0 1 2... n-1 EOF A file with n bytes.

Stream Provides communication channel between files and programs Is created when a file is opened Function feof() Tests a stream for end-of-file. Prototype: int feof(file *stream); Returns nonzero if and only if the end-of-file indicator is set for the stream argument.

Opening and Closing Files An object of type FILE *, is created by calling the function fopen() Then the file can be accessed by other file manipulation functions such as fprintf() and fscanf(). 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 the stream. Argument mode specifies the meaning of opening a file. The valid values for this argument are described in the following table:

r w 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+ Append; open or create a binary file for read/write. You can read data anywhere in the file, but you can write data only at the end-of-file.

If the 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 stream to be flushed and the associated file to be closed. It returns 0 when successful. Otherwise, it returns non-zero value.

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 for reading (starts at beginning) */ if((fpt2 = fopen( filename, r )) == NULL) printf("cannot open the file\n");... fclose(fpt1); fclose(fpt2);

There are 3 special files: standard input, standard output, and standard error. They 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: stdin - standard input (keyboard) stdout - standard output (screen) stderr - standard error (screen)

Reading and Writing Characters fgetc() reads one character from a file int fgetc(file *stream); fgetc(stdin) is equivalent to getchar() fputc() writes one character to a file int fputc(int c, FILE *stream); fputc('a', stdout) is equivalent to putchar('a')

/* Function that copies a file char by char */ 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); return 0; Why binary? To avoid complications due to newline representations in different OS

Reading and Writing Lines fgets() reads a line from a file char *fgets(char *s, int n, FILE *stream); fputs() writes a line to a file int fputs(const char *s, FILE *stream);

/* File: copyfile2.chf for copying a file line by line */ #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); return 0;

General Reading and Writing fscanf/fprintf are the file processing equivalents of scanf and printf int fprintf(file *stream, const char *format,...); int fscanf(file *stream, const char *format,...); The function calls printf(format, arglist); scanf(format, arglist); are equivalent to fprintf(stdout, format, arglist); fscanf(stdin, format, arglist);

Sample Problem The system in Figure1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is. The freebody diagram for the system is shown in Figure1 (b). Figure1: The system diagram and FBD of a sample problem

Read and understand Program 14.3 / 568 /* 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 */ /* 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"); return 0;

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

SKIP the remainder of this chapter This is the end of the material required for the final!