LAB 13 FILE PROCESSING

Similar documents
LAB 13 FILE PROCESSING

C File Processing: One-Page Summary

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

Introduction to Computer Programming Lecture 18 Binary Files

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

Ch 11. C File Processing (review)

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

Chapter 11 File Processing

Lecture6 File Processing

ENG120. Misc. Topics

C: How to Program. Week /June/18

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

Lecture 9: File Processing. Quazi Rahman

STRUCTURES & FILE IO

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

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

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$13$

BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$13$

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 13

System Software Experiment 1 Lecture 7

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

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

Standard File Pointers

Computer Programming Unit v

Fundamentals of Programming. Lecture 15: C File Processing

Standard C Library Functions

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

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

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

C PROGRAMMING. Characters and Strings File Processing Exercise

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

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

Input / Output Functions

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

CS240: Programming in C

Data File and File Handling

C programming basics T3-1 -

C Basics And Concepts Input And Output

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

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

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

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

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

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

UNIX System Programming

Input/Output and the Operating Systems

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

Intermediate Programming, Spring 2017*

Lecture 8: Structs & File I/O

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

Introduction to file management

Goals of this Lecture

I/O Management! Goals of this Lecture!

File I/O BJ Furman 23OCT2010

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

I/O Management! Goals of this Lecture!

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

Input/Output, Libraries, and Files 2000 UW CSE

C introduction: part 1

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

EM108 Software Development for Engineers

UNIX input and output

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

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

Chapter 8 File Processing

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

C-Refresher: Session 10 Disk IO

File IO and command line input CSE 2451

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Computer System and 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

HIGH LEVEL FILE PROCESSING

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

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Classes and Objects. Instructor: 小黑

DATA STRUCTURES USING C

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Lab # 4. Files & Queues in C

25.2 Opening and Closing a File

The Design of C: A Rational Reconstruction: Part 2

CAAM 420 Notes Chapter 2: The C Programming Language

C mini reference. 5 Binary numbers 12

M.CS201 Programming language

2009 S2 COMP File Operations

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

Advanced C Programming and Introduction to Data Structures

EE458 - Embedded Systems Lecture 4 Embedded Devel.

File Handling. 21 July 2009 Programming and Data Structure 1

Computer programming

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

Computer Programming

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

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

The Design of C: A Rational Reconstruction (cont.)

Transcription:

LAB 13 FILE PROCESSING School of Computer and Communication Engineering Universiti Malaysia Perlis 1

1. OBJECTIVES: 1.1 To be able to create, read, write and update files. 1.2 To become familiar with sequential access file processing. 1.3 To become familiar with random-access file processing. 2. INTRODUCTION: A file is a group of related records and it is used to store large amounts of data for permanent retention. A group of related files is called a database. Computers store files on secondary storage devices, especially disk storage devices. Two ways of organizing, accessing and processing records in a file; sequential-access file and random-access file. 2.1 Declaring File Pointer Variables A file pointer variable has to be declared and initialized before a file can be assessed and used. The following statements declare the file pointer variables infilep and outfilep: FILE *infilep; FILE *outfilep; Notice that the data type of infilep and outfilep is FILE*-a pointer to FILE. FILE is a structure type defined by the stdio library with components to hold information needed for file access. 2.2 Opening and Closing a File fopen function is used to open a file and the general format to open a file is given as: For example: internal_filename = fopen (external_filename, mode); infilep = fopen ( data.txt, r ); outfilep = fopen ( results.txt, w ); fclose function is used to close a file. The general format to close a file is given as: For example: fclose (internal_filename); fclose (infilep); fclose (outfilep); feof function can be used to determine whether the EOF* has been reached. *EOF or end-of-file is an indicator that marks the end of the entire file. For computers running the Linux/UNIX OS, <control-d> is used as EOF. 2.3 Writing to and Reading from a File fprintf and fwrite functions are used to write data to a file : For example: fprintf (outfilep, %d, number); fwrite (&client, sizeof(struct clientdata), 1, outfilep); address number of bytes to copy to file file pointer number of values to be written to file fscanf and fread functions are used to read data from a file: For example: fscanf(infilep, %d, &number); fread(&client, sizeof(struct clientdata), 1, infilep); 2

3. TASKS: 3.1 Answer the following questions: a. Declare a variable pointer type of FILE named cfptr. b. Write a statement that opens a file name input.txt for reading and assigns the returned file pointer to cf1ptr. c. Write a statement that opens a file name output.txt for writing (and creation) and assigns the returned file pointer to cf2ptr. d. Write a statement that reads a record from the file input.txt. The record consists of long integer MatrixID, string name and floating point cgpa. e. Write a statement that writes a record to the file output.txt. The record consists of integer month and floating point avg_temp. f. State the usage of the following functions: fgetc fgets fread fseek 3.2 Sequential Access File: The following program creates a sequential file named student.txt. The program will get information from keyboard and write the information into the file student.txt. #include<stdio.h> long int MatrixID; char name[30]; float cgpa; //file pointer declaration if ((cfptr = fopen("student.txt", "w")) == NULL) //create and open file for writing printf("\nfile can't be opened"); printf("enter Matrix No, Name and CGPA\n"); printf("enter EOF to end input\n"); printf(": "); scanf("%ld %s %f", &MatrixID, name, &cgpa); while (!feof(stdin)) //check for end of file (EOF) fprintf(cfptr, "%ld %s %.2f\n",MatrixID, name, cgpa); //write into file printf(": "); scanf("%ld %s %f", &MatrixID, name, &cgpa); //get data from //keyboard 3

//close file stdin stdout stderr student.txt - is a standard input stream. A system file pointer for keyboard s input stream. - is a standard output stream. A system file pointer for screen s output stream. - is an error output stream. - is a text file that is created and saved in the hard disk. Find the file student.txt in the same directory of your program run. Open the file and you will see the data that you have keyed in. Check your output in the file student.txt. 3.3 Sequential Access File: The following program reads data from a sequential file and displays the data to the monitor / screen. For example, we have a file name student.txt, and we want to read the contents of the file and display the contents on the screen. long int MatrixID; char name[30]; float cgpa; //file pointer declaration if ((cfptr = fopen("student.txt", "r")) == NULL) //open file for reading printf("\nfile can t be opened"); printf("%-13s%6s%16s\n", "Matrix No", "Name", "CGPA"); //write a title or //header //read contents from file and load them to variable MatrixID, name, and cgpa fscanf(cfptr, "%d %s %f", &MatrixID, name, &cgpa); while(!feof(cfptr)) //check for end of file (EOF) printf("%-15d%-13s%7.2f\n", MatrixID, name, cgpa); //display data //to screen fscanf(cfptr, "%d %s %f",&matrixid, name, &cgpa); //read data //from file a. Check whether the data displayed are the same as in the input file. Write down the output Let say the contents of the file are: 90812345 Ahmad 3.5 90854321 Salina 3.9 90823456 Lee 3.75 90865432 Jason 3.3 4

The output will be displayed as: Matrix No Name CGPA 90812345 Ahmad 3.5 90854321 Salina 3.9 90823456 Lee 3.75 90865432 Jason 3.3 3.4 Random Access File: a. The following program creates a random access file. This program creates 10 blank records to a file named credit.dat. Type, compile and run the following program. struct clientdata int acctnum; char lastname[15]; char firstname[15]; float balance; ; //clientdata structure definition int main( ) int i; //file pointer declaration //initialize all the structure members with 0 and blanks struct clientdata client = 0, "", "", 0.0; if((cfptr = fopen("credit.dat", "w")) == NULL) printf("\nfile can't be opened"); //write 10 blanks records to file credit.dat for (i = 1; i<=10; i++) fwrite(&client, sizeof(struct clientdata), 1, cfptr); Open the file credit.dat. What is the content of credit.dat? b. The previous program in (a) illustrates how to write data sequentially to a random access file, now, the following programs in (b) and (c) show how to write data randomly to a random access file and how to read data from the file. Type, compile and run both programs. struct clientdata int acctnum; char lastname[15]; char firstname[15]; float balance; ; 5

struct clientdata client; if((cfptr = fopen("credit.dat", "r+")) == NULL) printf("file can t be opened"); printf("\nenter account number(1 to 100, 0 to end input): "); scanf("%d", &client.acctnum); //enter data to be copied into file while (client.acctnum!= 0) printf("enter client s last name, first name and balance: "); fscanf(stdin, "%s %s %f", client.lastname, client.firstname, &client.balance); //seek position in file to user-specified record fseek(cfptr, (client.acctnum - 1) * sizeof(struct clientdata), SEEK_SET); //write user-specified information in file fwrite(&client, sizeof(struct clientdata), 1, cfptr); printf("\nenter account number(1 to 100, 0 to end input): "); scanf("%d", &client.acctnum); c. The following program shows how to read data from a random access file. struct clientdata int acctnum; char lastname[15]; char firstname[15]; float balance; ; struct clientdata client; if((cfptr = fopen("credit.dat", "r")) == NULL) printf("file can t be opened"); printf("%-6s%16s%15s%10s\n", "Account No", "Last Name", "First Name", "Balance"); //read all records from file (until eof) while (!feof(cfptr)) fread(&client, sizeof(struct clientdata), 1, cfptr); 6

fclose (cfptr); //display records on screen if (client.acctnum!= 0) printf("%-6d%16s%15s%14.2f\n", client.acctnum, client.lastname, client.firstname, client.balance); Compare the data on the screen. Is the data displayed same as your input data? 3.5 Problem Solving: As an engineer for air traffic controller, you are required to develop a system that will alert the air traffic controller of incoming aircraft. You will have to write two programs:- - a program to store the coordinates of incoming aircrafts into a file (input file) and - another program to read and use the data in the file to calculate the distance of each aircraft from the control center. The distance is then saved and stored in another file (output file). If the distance calculated is less than 20 km, write an alarm message to the screen that will inform the number of the incoming aircraft. Distance formula = sqrt (x 2 + y 2 ). (Note: use #include <math.h> in your program, and use : gcc lm programfilename.c to compile). For example: Input File: Aircraft Coordinate (km) x y A 25.0 20.0 B 10.0 16.0 C 8.0 16.0 D 136.0 85.0 Output File: Aircraft Distance from Control Center (km) A 32.02 B 18.87 C 17.89 D 160.38 Output on screen: Incoming Aircraft(s): 2 7