HIGH LEVEL FILE PROCESSING

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

Computer programming

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

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

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

File IO and command line input CSE 2451

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

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

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

Input / Output Functions

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Input/Output and the Operating Systems

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

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

Data File and File Handling

File I/O. Preprocessor Macros

EM108 Software Development for Engineers

Standard C Library Functions

Standard File Pointers

C-Refresher: Session 10 Disk IO

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

2009 S2 COMP File Operations

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

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

PROGRAMMAZIONE I A.A. 2017/2018

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

25.2 Opening and Closing a File

UNIX System Programming

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

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

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

Computer Programming Unit v

Lecture 9: File Processing. Quazi Rahman

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

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

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

Summer June 15, 2010

System Software Experiment 1 Lecture 7

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

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

File Handling. Reference:

CS240: Programming in C

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

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

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

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

M.CS201 Programming language

Intermediate Programming, Spring 2017*

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

C Programming Language

Computer Programming: Skills & Concepts (CP) Files in C

Engineering program development 7. Edited by Péter Vass

Goals of this Lecture

Lab # 4. Files & Queues in C

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

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)

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

C programming basics T3-1 -

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

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

Operating System Labs. Yuanbin Wu

Memory Management and

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

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

Advanced C Programming and Introduction to Data Structures

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

Laboratory: USING FILES I. THEORETICAL ASPECTS

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

ENG120. Misc. Topics

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

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

Input/Output: Advanced Concepts

Ch 11. C File Processing (review)

File and Console I/O. CS449 Spring 2016

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

C File Processing: One-Page Summary

Contents. A Review of C language. Visual C Visual C++ 6.0

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSCI 171 Chapter Outlines

Programming Fundamentals

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

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

File System User API

Programming. Functions and File I/O

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

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

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

C Basics And Concepts Input And Output

CE Lecture 11

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

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

Introduction to file management

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

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

Transcription:

HIGH LEVEL FILE PROCESSING 1. Overview The learning objectives of this lab session are: To understand the functions used for file processing at a higher level. o These functions use special structures of type FILE. o The main operations which can be performed on files at this level are: open (including to create a new file), read/write of a character or a character string, read/write binary, file positioning, file close, flushing the buffer area for a file. To acquire hands-on experience in using high level IO functions. 2. Brief theory reminder At the higher level of the file management system, a pointer to a structure of type FILE is attached to each file: FILE *p; The type FILE and all function prototypes are found in stdio.h. 2.1. File open To open an existing file, and also to create a new one you can use the function fopen, with the following prototype: FILE *fopen(const char *path_name, const char *mode); path_name pointer to a character string which defines the path name for a file; mode pointer to a character string which defines the file processing mode for the opened file as follows: r open for reading; w open for writing; a open for appending; r+ open for updating (both read and write); rb open for binary reading; wb open for binary writing; r+b open for binary reading/writing ; w+b open for binary writing/reading; ab open for binary appending. Notes. - The content of an existing file opened for writing (mode w ) will be overwritten. - If a file is opened in mode append (mode a ), new records can be added after the last existing record in the file. - A non-existent file opened in modes w or a will be automatically created.. - The function fopen returns a pointer of type FILE if successful, or the NULL pointer if not. The standard I/O files are automatically opened at run time. They are: stdin standard input; stdout standard output; stderr standard error; On a PC, in MS-DOS the following are also opened: T.U. Cluj-Napoca Computer Programming 1

stdprn standard printing output; stdaux standard auxiliary device (serial communication). 2.2. Character-wise processing of a file 2.2.1. Read/write character by character A file can be processed by reading opr writing character by character, using functions getc and putc, of prototypes: int getc (FILE *pf); int putc (int ch, FILE *pf); pf - is a pointer to the type FILE returned by the function fopen; ch - is the ASCII code of the character to write. Both return the ASCII code of a character (read or written) on success; on error -1 is returned. For example, a sequence used to copy the standard input to the standard output is: while ((c=getc(stdin))!=eof) putc(c, stdout); 2.2.2. Read/write character strings To read a character string form a file use fgets. This has the prototype: char *fgets(char *s, int n, FILE *pf); s a pointer to the memory area where the character string will be stored; n the size of the memory area where the string will be read. Reading stops when the EOL (End Of Line, \n ) is met or at most n-1 characters were read. In both cases, the last character in the string will be \0. Pf a pointer to a structure of type FILE. fgets returns the value of s on success, and a NULL pointer on failure. To write a character string, ( \0 is included) use the function fputs, of prototype: int fputs (const char *s, FILE *pf); where s is a pointer to the beginning of the memory area containing the string to be written. fputs returns the ASCII code of the last written character if successful, or 1 on failure. 2.2.3. Formatted read/write Formatted read/write can be performed using the functions fscanf/fprintf, similar to the functions sscanf/sprintf, presented in laboratory session no. 1. The difference consists in the fact that for sscanf/sprintf the first argument is a pointer to the area where the character string will be stored, and for fscanf/fprintf the first argument is a pointer to the file used for reading/writing as their prototypes show: int fscanf(file *pf, const char *format, address ); int fprintf(file *pf, const char *format, expression ); T.U. Cluj-Napoca Computer Programming 2

fscanf returns the number of correctly read fields. When the end of a file is met, fscanf returns EOF. fprintf returns the number of characters written to the file if successful, and 1 if it fails. 2.3. Flushing a file buffer To flush a file buffer, use fflush, which has the following prototype: int fflush(file *pf); where pf is a pointer to a structure of type FILE, opened for output. Notes. - If the file is opened for writing, the contents of its buffer area is written to the corresponding file. - If the file is opened for reading, the unread characters are lost. - fflush returns zero upon success, and 1 if an error occurs. 2.4. Positioning in a file To position the file pointer on a certain byte use fseek. Prototype: int fseek (FILE *pf, long offset, int origin); This function is similar to lseek. The difference consists in the fact that for fseek one passes a pointer to type FILE as the first argument, and for lseek a file descriptor was given. The current position in the file is given as an offset in bytes from its beginning is returned by the function ftell. Its prototype is: long ftell(file *pf); 2.5. Processing binary files Binary files are considered to be sequences of records, each block containing a number records, as show below: RECORD RECORD RECORD... RECORD RECORD RECORD EOF block block Records have fixed lengths. A record is data of predefined or user defined type. To read/write records use fread/fwrite. Prototypes: unsigned fread(void *buf, unsigned dim, unsigned nrart, FILE *pf); unsigned fwrite(void *buf, unsigned dim, unsigned nrart, FILE *pf); buf pointer to the buffer area containing the read/to write records; dim size of a record in bytes; nrart number of records in a block; pf pointer to structure of type FILE. Both functions return the number of records read/written on success, and 1 on error.. 2.6. File close To close a file use fclose. Prototype: T.U. Cluj-Napoca Computer Programming 3

int fclose(file *pf); where pf is a pointer to a structure of type FILE returned by fopen. The function returns 0 if successful close, and 1 if not. 2.7. File delete A closed file can be deleted using the function unlink, of prototype: int unlink (const char *path_name); where path_name is a pointer to a character string representing the path to the file.the function returns 0 if successful close, and 1 if not. 2.8. Examples 2.2.4. Example 1. Character and string oriented file processing. Program L11Ex1.c creates a file character by character, the characters being read from the standard input. Then it illustrates the way to append character strings, and then it prints the file, line by line, lines being preceded by their line numbers. /* Program L11Ex1.c */ #include <stdio.h> /* Shows character and character string oriented file processing*/ int main(void) char ch, s[100], file_name[50]="file1.txt"; int i; FILE *pf; /* create a file; write all characters read from stdin (including \n */ pf=fopen(file_name, "w"); printf("\nplease input a text to store in file \ %s\.\nend with Ctrl+Z\n", file_name); while ((ch=getc(stdin))!=eof) putc(ch, pf); /* append character strings*/ pf=fopen(file_name,"r+"); fseek(pf, 0l, 2); printf("\nplease input the strings to append to the created file. End with an empty\ line.\n ); while(fgets(s, 100, stdin)!=(char*)0) fputs(s, pf); /* show file contents */ T.U. Cluj-Napoca Computer Programming 4

printf("\nlines of the files (numbered):\n"); i=0; pf=fopen(file_name,"r"); while(fgets(s,100,pf)!=(char *)0) printf("%d %s", i, s); i++; getchar(); unlink(file_name); return 0; 2.2.5. Example 2. Binary processing of files. Program L11Ex2.c illustrates binary processing of files. It creates and then lists the contents fof the file. /* Program L11Ex2.c */ #include <stdio.h> /* Illustrates binary file processing */ typedef struct char name[40]; long sum; /*other members */ RecordT; void show_contents(char *file_name) FILE *pf; RecordT buf; int i; pf=fopen(file_name, "rb"); printf("\nno. SUM FAMILY NAME-FIRST NAME\n"); i=0; while(fread(&buf, sizeof(recordt), 1, pf) > 0) printf("\n%6d %10ld %-40s", i, buf.sum, buf.name); i++; int main(void) FILE *pf; RecordT buf; int i, n; char s[40], file_name[40]="file.dat"; /*Create file */ printf("\ninput the number of persons, n="); T.U. Cluj-Napoca Computer Programming 5

scanf("%d", &n); pf=fopen(file_name, "wb"); for(i=1; i<=n; i++) printf("family name and first name of the person:: "); fgets(buf.name, 40, stdin); printf("sum = "); scanf(" %ld", &buf.sum); fwrite(&buf, sizeof(recordt), 1, pf); printf("\nfile contents:\n"); show_contents(file_name); getchar(); return 0; 3. Lab Tasks 3.1. Analyze and execute the examples L11Ex1.c and L11Ex2.c. 3.2. Create a file containing a list of items a shop sells. An item is represented as a structure which contains: - a code; - a name; - a unit of measure; - the amount; - price per unit 3.3. Starting with this file get another one where the items are sorted ascending on their code field. 3.4. Using the file created at 3.2, write functions to input and output items in the shop. 3.5. Write a program to process the results of the admission exam at our University. The program shall include the creation of the file with candidate data. Finally, you must obtain the files with the candidates admitted for each specialization in the order of their average scores and the ones rejected in order of their names. An average score is calculated using the formula: ((baccalaureate + 4 * math_test / 5.0). 3.6. Consider a file directory. Each entry contains a 8 character name and a 3 character extension, the number of blocks allocated to store it, the address of the first allocated block, and the date and time of last update (year, month, day, hour, minute, second). You shall: - Create a file directory. - List the files in the directory in the ascending order of file names. - List the files in the directory in the ascending order of their last update date and time. 3.7. Write programs which use the high level functions to implement problems 3.2.-3.10 of laboratory session no. 10. T.U. Cluj-Napoca Computer Programming 6