DKT121: Fundamental of Computer Programming

Slides:



Advertisements
Similar presentations
UNIT 12 UNIX I/O Redirection.
Advertisements

C File Processing - I Math 130 B Smith: Eval: 3. Energy and excitement helped for avg graphics. Gen interest. A bit wordy, henced redundant in some parts.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
1 CSSE 332 Structures, Command Line Arguments. 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4]
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 CSE1301 Computer Programming: Lecture 21 File I/O.
Informationsteknologi Monday, September 10, 2007Computer Systems/Operating Systems - Class 31 Today’s class Review of more C Operating system overview.
CSE1301 Computer Programming: Lecture 19 File I/O
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
1 CSE1301 Computer Programming: Lecture 19 File I/O.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
CSE1301 Computer Programming: Lecture 14 I/O and Files.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming1 Files Operations.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
File Input/Output. File information Name z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt \\file-svr-1\class\ECE-160\pviall\myoutput.txt Read/Write.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
ECE Application Programming
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
ECE Application Programming
Structured Programming II
Lecture 8: Variable Scope & Working with Files
EKT120: Computer Programming
CS1010 Programming Methodology
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Week 4 – Repetition Structures / Loops
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
What you need for the 1st phase of project
CSE1320 Files in C Dr. Sajib Datta
Lesson #5 Repetition and Loops.
Interactive I/O Input from keyboard Must prompt user User friendly
CSE1320 Strings Dr. Sajib Datta
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
CS150 Introduction to Computer Science 1
Files.
CPS120: Introduction to Computer Science
FILE handeling.
A First Book of ANSI C Fourth Edition
EECE.2160 ECE Application Programming
Files Operations.
Chapter 11 Files chap8.
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

DKT121: Fundamental of Computer Programming Files Operations UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard and output is via screen/monitor This type of processing is not suitable if it involves huge amount of input or output to be entered or be displayed on the screen at one time Therefore, file processing can solve the problem mentioned UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming File Declaration To implement file processing in C program, need to #include <stdio.h> FILE *in_file; FILE *out_file; in_file and out_file are known as internal file name UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Opening File and fopen function Each file must be opened before it is processed While opening the file, external file name need to be related to the internal file name using fopen function. Format: internal_filename =fopen(external_filename, mode); Internal file name is the name that the C system uses to identify a file among others that a program might process External file name is the name given at “save file as” outside the program e.g. “student.dat”, “student.out” etc Mode is used to open file UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming File Mode Basics mode are: “r” : open file to read “w” : open file to write “a” : append data to the end of an already existing file “r+” : open and create file to update, i.e. read and write; did not overwrite previous output “w+” :open and create file for update; overwrite “a+” : append; open or create file for update UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Opening File and fopen function-example FILE *in_file; FILE *out_file; in_file = fopen(“student.dat”, “r”); out_file = fopen(“student.out”, “w”); UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

File Open Verification There is a possibility opening file fails. Maybe the particular file does not exist Therefore need to check or verify whether the file is successfully opened If file fails to open, need to stop the program, use exit(-1); if (in_file == NULL) { printf(“File fails to open\n”); exit(-1); } UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

File Open Verification-cont You can also combine open file and file verification if ((in_file = fopen(“student.dat”, “r”)) == NULL) { printf(“File fails to open\n”); exit(-1); } UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Closing File and fclose function Each opened file need to be closed Format: fclose(internal_filename); e.g fclose(in_file); fclose(out_file); UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Check for End-of-File and the feof function Usually you don’t know how many data you want to read from file Therefore, need to check whether you have reach end of file Format: feof(internal_filename) UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Check for End-of-File and the feof function-example FILE *in_file; in_file = fopen(“student.dat”, “r”); if(in_file == NULL) { printf(“Error opening file\n”); exit(-1); } while(! feof(in_file)) { //stmt to process data } fclose(in_file); UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Reading Data from Text Files Format fscanf(internal file name, format control string, input list); fscanf(in_file, “%d”, &marks); fgetc(internal file name); ch = fgetc(in_file); fgets(string variable, size, internal file name); fgets(name, 10, in_file); UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Writing Data to Text File Format fprintf(internal file name, format control string, output list); fprintf(out_file, “%d”, marks); fputc(character expression,internal file name); fputc(ch, out_file); fputc(“4”, out_file); fputs(string expression, internal file name); fputs(name, out_file); fputs(“makan”, out_file); UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Sample Program while(!feof(in_file)) { fscanf(in_file,"%d",&marks); ++count; total = total + marks; fprintf(out_file, " %d ",marks); } avg = total /count; fprintf(out_file, "\n%.2f\n", avg); fclose(in_file); fclose(out_file); return 0; }//main #include <stdio.h> #include <stdlib.h> int main(void) { FILE *in_file; FILE *out_file; int marks, total=0, count = 0; float avg; in_file = fopen("student.dat", "r"); out_file= fopen("student.out", "w"); if(in_file == NULL) printf("Error opening file\n"); exit(-1); } UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

Sample input file and output file student.dat 50 60 70 80 90 44 55 66 77 88 24 56 79 50 77 student.out 50 60 70 80 90 44 55 66 77 88 24 56 79 50 77 64.00 UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming End Files Operation Q & A! UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming