EM108 Software Development for Engineers

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

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

2009 S2 COMP File Operations

System Software Experiment 1 Lecture 7

Data File and File Handling

UNIX System Programming

Standard File Pointers

PROGRAMMAZIONE I A.A. 2017/2018

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

File Handling. Reference:

25.2 Opening and Closing a File

M.CS201 Programming language

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

Engineering program development 7. Edited by Péter Vass

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

C Programming Language

Input/Output and the Operating Systems

Input / Output Functions

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

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

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

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

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

COMP1917: 15 File IO

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Computer Programming Unit v

File I/O. Preprocessor Macros

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

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

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

Computer System and programming in C

HIGH LEVEL FILE PROCESSING

Laboratory: USING FILES I. THEORETICAL ASPECTS

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

C-Refresher: Session 10 Disk IO

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

File IO and command line input CSE 2451

EM108 Software Development for Engineers

Introduction to file management

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

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

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

CS240: Programming in C

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

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

Lecture 9: File Processing. Quazi Rahman

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

Naked C Lecture 6. File Operations and System Calls

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

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

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

Introduction to Computer Programming Lecture 18 Binary Files

Data Files. Computer Basics

Course organization. Course introduction ( Week 1)

ENG120. Misc. Topics

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

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)

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

C File Processing: One-Page Summary

Programming Fundamentals

Strings. Part III. 1) Introduction. 2) Definition. 3) Using Strings. 4) Input/Output. 5) Using string.h Library. 6) Strings as Function Parameters

Computer programming

Goals of this Lecture

Simple Output and Input. see chapter 7

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Chapter 10. File Processing 248 FILE PROCESSING

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

File and Console I/O. CS449 Spring 2016

The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

Ch 11. C File Processing (review)

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

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

Text Output and Input; Redirection

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

Organization of a file

C Basics And Concepts Input And Output

CSI 402 Lecture 2 (More on Files) 2 1 / 20

UNIT- 2. Binary File

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

CE Lecture 11

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

Intermediate Programming, Spring 2017*

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

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

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

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Input/Output: Advanced Concepts

Standard C Library Functions

C programming basics T3-1 -

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

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

Chapter 11 File Processing

EM108 Software Development for Engineers

Programming. Functions and File I/O

Operating System Labs. Yuanbin Wu

Transcription:

EE108 Section 4 Files page 1 of 14 EM108 Software Development for Engineers Section 4 - Files 1) Introduction 2) Operations with Files 3) Opening Files 4) Input/Output Operations 5) Other Operations 6) Closing Files 7) Complex example

EE108 Section 4 Files page 2 of 14 File: Need: 4.1 Introduction A collection of data stored on the disk Input from keyboard - serious limitation - data has to be typed again each time the program is run Output to screen serious limitation - data is lost when the program ends Notes: Data access requires file reading and writing operations Data access to/from disk is slower than to/from memory Data is saved even after the program execution ends Data can be re-used File types (in C): Binary - stores data in binary format - e.g. 23 is stored as the integer 23 represented in binary: 00010111 Text (implicit) - stores data in ASCII format - e.g. 23 is stored as two characters: 2 and 3

EE108 Section 4 Files page 3 of 14 Operations: Open files: 4.2 Operations with Files fopen - opens a file Input/Output operations: fscanf - reads data from a file getc - reads a character from a file ungetc - returns a read character to the file fgets - reads a string from file fprintf - writes data to a file putc - writes a character to a file fputs - writes a string to a file Other operations fseek - sets the cursor (position) into a file feof - tests for the end of file fflush - flushes the buffer associated to a file Close file fclose - closes an opened file Notes: Any file has to be opened before input/output operations are allowed Data is stored and can be accessed only sequentially in files

EE108 Section 4 Files page 4 of 14 Definition Notes 4.3 Opening Files FILE* fopen(char* file_name, char* mode); file_name indicates the name of the file to be opened file_name can be provided relative to the current directory or could include the absolute path to the file mode indicates what operations are allowed on the file and the type of the file - operations: w - write only (in front of file) r - read only (from front of file) a - append (at the end of file) - file type: t - text (implicit) b - binary fopen associates a file descriptor to the opened file that can be used for any operations with the file fopen returns a pointer to the file descriptor Example: FILE* fp; fp = fopen(file_name, r ); fp = fopen( my_file.txt, rt ); fp = fopen( c:\\ee105\\my_file.txt, wb );

EE108 Section 4 Files page 5 of 14 4.4 Input/Output Operations Input Operations: int fscanf(file* fp, char* format, *[param]); - reads formatted data from a file - returns no of params read or EOF if end of file is encountered int getc(file* fp); - reads a character from a file and returns it int ungetc(int c, FILE* fp); - returns a read character to the file char* fgets(char* s, int n, FILE* fp); - reads a string of maximum n-1 characters from a file or until \n was encountered and adds \0 at the end - returns the string read Output Operations: int fprintf(file* fp, char* format, *[param]); - writes formatted data to a file - returns no parameters written or EOF if unsuccessful int putc(int c, FILE* fp); - writes a character to a file - returns the character written or EOF if unsuccessful int fputs(char* s, FILE* fp); - writes a string to a file - returns EOF if unsuccessful

EE108 Section 4 Files page 6 of 14 Example 1: FILE* fp; int balance; fp = fopen( my_file.txt, r ); if (fscanf(fp, %d, &balance)!= EOF) else printf( Balance is %d\n!, balance); printf( End of file encountered\n ); Example 2: FILE* fp; int balance; int ret_val; fp = fopen( my_file.txt, w ); printf( Enter balance: ) scanf( %d, &balance); ret_val = fprintf(fp, %d, balance); if (ret_val!= EOF) else printf( Balance written in file! ); printf( Error writing in file\n );

EE108 Section 4 Files page 7 of 14 Input Operations: 4.5 Other Operations int fseek(file* fp, long offset, int whence); - sets the cursor in the file indicated by fp at position offset starting from whence - whence could have the following values: SEEK_SET (=0) beginning of file SEEK_CUR (=1) current pos. in file SEEK_END (=2) end of file int feof(file* fp); - tests for the end of file in the file indicated by fp - returns a non-zero value if end of file was encountered and 0 otherwise int fflush(file* fp); - flushes the buffer associated to a file Example: FILE* fp; int balance; fp = fopen( my_file.txt, r ); if (!feof(fp)) else fscanf(fp, %d, &balance); printf( End of file encountered\n ); fflush(fp);

EE108 Section 4 Files page 8 of 14 Definition Notes 4.6 Closing Files int fclose(file* fp); fp is the pointer to the file descriptor returned by the function that opened the file frees the buffer associated to the opened file returns 0 if successful and EOF (=-1) otherwise Example: FILE* fp; int balance; float amount; fp = fopen( my_file.txt, r ); if (!feof(fp)) fscanf(fp, %d, &balance); else printf( End of file encountered\n ); fclose(fp); printf( Enter the amount: ); scanf( %f, &amount); balance = balance + amount; fp = fopen( my_file.txt, w ); fprintf(fp, %d, balance); fflush(fp); fclose(fp);

EE108 Section 4 Files page 9 of 14 4.7 Complex Example Example: A file named grades.txt stores the following information in text format: student_id grade_s1 grade_s2 The data represents student IDs and student grades for two subjects There are no more than 5000 records Read the student IDs and store them in the array studentids[], the grades for the first subject and store them in the array ee102[] and the grades for the second subject and store them in the array ee105[] Process the grades from the arrays ee102[] and ee105[], compute the average for each student and store the results in another array named avggrades[] Print on the screen the results for all the students Write data in the following format in a new file called results.txt student_id grade_s1 grade_s2 average Use four functions : read_records() compute_average() print_results() write_records()

EE108 Section 4 Files page 10 of 14 #include <stdio.h> #include <stdlib.h> /* declarations */ /* function that reads data from a file, stores it in three arrays and returns the number of records read*/ int read_records (char* file_name, int studids[], int ee102[], int ee105[]); /* function that computes the average for each student given two arrays with grades and stores the results in a third array */ void compute_average (int ee102[], int ee105[], int avg[], int size); /* function that writes data taken from four arrays into a file */ void write_records (char* file_name, int studids[], int ee102[], int ee105[], int avg[], int size); /* function that prints the studentids, grades and their average marks */ void print_results (int studids[], int ee102[], int ee105[], int avg[], int size); /* main function */ int main() { /* 1-dimensional array that stores studentids for all registered students */ int studentids[5000]; /* 1-dimensional array that stores average marks for all registered students */ int avggrades[5000];

EE108 Section 4 Files page 11 of 14 /* 1-dimensional array that stores grades for subject ee102 for all registered students */ int ee102[5000]; /* 1-dimensional array that stores grades for subject ee105 for all registered students */ int ee105[5000]; /* no current registered students (no records) */ int norecs = 0; /* welcome message */ printf( Welcome to the program!\n ); /* read records */ norecs = read_records( grades.txt, studentids, ee102, ee105); /* compute the averages */ compute_average(ee102, ee105, avggrades, norecs); /* write data to a file */ write_records( results.txt, studentids, ee102, ee105, avggrades, norecs); /* print result s */ print_results(studentids, ee102, ee105, avggrades, norecs); printf( Goodbye!\n ); } return (EXIT_SUCCESS);

EE108 Section 4 Files page 12 of 14 /* function definitions */ int read_records (char* file_name, int studids[], int ee102[], int ee105[]) { FILE* fp; int norecs = 0; char ch; /* open file */ fp = fopen(file_name, r ); while(!feof(fp)) { /* read on record from file */ fscanf(fp, %d%c%d%c%d%c, &studids[norecs], &ch, &ee102[norecs], &ch, &ee105[norecs], &ch); } /* increase the number of records read */ norecs++; /* close file */ fclose(fp); } return norecs; void compute_average (int ee102[], int ee105[], int avg[], int size) { int i; } /* parse grades and compute the average */ for (i = 0; i < size; i++) avg[i] = (ee102[i] + ee105[i])/2;

EE108 Section 4 Files page 13 of 14 void write_records (char* file_name, int studids[], int ee102[], int ee105[], int avg[], int size) { FILE* fp; int i; /* open file */ fp = fopen(file_name, w ); for (i = 0; i < size; i++) /* write a record in the file */ fprintf(fp, %d %d %d %d\n, studids[i], ee102[i], ee105[i], avg[i]); fflush(fp); } /* close file */ fclose(fp); void print_results (int studids[], int ee102[], int ee105[], int avg[], int size) { int i; printf( StudID ee102 ee105 Avg \n ); } /* parse data and print grades and average */ for (i = 0; i < size; i++) printf( %8d %3d %3d %3d \n, studids[i], ee102[i], ee105[i], avg[i]);

EE108 Section 4 Files page 14 of 14 grades.txt 60001 60 40 40200 70 70 50100 45 55 results.txt 60001 60 40 50 40200 70 70 70 50100 45 55 50 [0] [1] studentids 60001 40200... [0] [1] ee102 60 70... [0] [1] ee105 40 70... [0] [1] avggrades 50 70... [no_recs -1] 50100 [no_recs -1] 45 [no_recs -1] 55 [no_recs -1] 50 Main tasks: Read from the grades.txt file the following information for each student: StudentID, ee102 grade, ee105 grade - open grades.txt file in reading mode - read records line by line - store the data in studentids, ee102 and ee105 arrays - close the file Compute the average mark for each student using the ee102 and ee105 arrays and store the values in the avggrades array Write the following information for each student in the results.txt file: studentid, ee102 grade, ee105 grade and avg. mark - open results.txt file in writing mode - write records line by line - close the file Print on the screen the following information about each student by parsing the arrays: StudentID, ee102 grade, ee105 grade and average mark