Structured Programming II

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
UNIT 15 File Processing.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
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.
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.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
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.
CSE1301 Computer Programming: Lecture 14 I/O and Files.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 3 Input and Output
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:
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
By C. Shing ITEC Dept Radford University
DKT121: Fundamental of Computer Programming
Chapter 7 Text Input/Output Objectives
ECE Application Programming
Chapter 7 Text Input/Output Objectives
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
Chapter 7 Text Input/Output Objectives
Lecture 8: Variable Scope & Working with Files
EKT120: Computer Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CS111 Computer Programming
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
Programming in C Input / Output.
What you need for the 1st phase of project
Programming in C Input / Output.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CSE1320 Strings Dr. Sajib Datta
Chapter 2 - Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
File Handling.
Chapter 2 - Introduction to C Programming
Programming in C Input / Output.
Files.
Chapter 2 - Introduction to C Programming
Chapter 12: File I/O.
A First Book of ANSI C Fourth Edition
EECE.2160 ECE Application Programming
Files Operations.
Introduction to C Programming
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

Structured Programming II Text Files CHAPTER 2 Structured Programming II

Why we need Files Files are kept in Secondary Storage Units (Disks, Diskettes). Arrays use Primary Storage Units (RAM) of the computer. When you have to make a decision to use either a File or an Array you have to consider the following criterias: Speed of access to the data stored in. Unit Price of the Storage. Durability of the content. Amount of data have to be stored.

Why we need Files

Using Files in C Large amount of data are usually stored in files. Obtaining data (numbers or characters) from a file is called READING THE FILE. Placing data into a file is called WRITING TO THE FILE. PROCESSING A FILE, means obtaining data from a file and/or placing data into another. In order to process a file in C, you must first declare the FILE POINTER.

Declaration of a File Format : FILE * file_pointer_name; The second step is the OPENING of the declared file. File_pointer_name = fopen( file_name, Opening_mode); Example : my_file = fopen(“MYFILE.TXT”, “r+”);

File Opening Modes Op. Mode: Meaning: “r” : Read only obtain data from an existing file. “w” : Write only, place data onto a new file. “a” : Append only, appends data to the end of a new or existing file. “r+” : Read and Write “w+” : Write and Read “a+” : Append and read Once program completes its processing with a file, it has to close the used file with fclose() statement. Example : flose(input_file); /* “input_file” is a File Pointer */

“fopen()” Function fopen() takes two arguments: file name. opening mode. fopen returns a NULL pointer when it fails to open a file. Use the following code in order to test the file is opened successfully or not: Example : input_file = fopen(“Myfile.txt”,”r”); if (input_file==NULL) { printf(“\n An Error occurred, Myfile.txt can not opened !”); exit(1); } print(“\n Myfile.txt is opened successfully “);

“fscanf()” Function fscanf() is used to read data from a file. fscanf() function is similar with scanf() function, but needs a file pointer as the first argument. While scanf() is reading data from keyboard, fscanf() is used for reading data from a file. Example : fscanf(input_file,”%d”,&num); sum+= num;

Examples Example 1: Write a program in C that will generate random 100 integer numbers between 0-500, and will write them into the file “SALES.DAT”. Example 2: Write a program in C that will read the sales data from the file “SALES.DAT” (containing integer values) and calculate tax amounts ( tax rate is given 25%) and write them into the file “TAX.DAT”. Example 3: Write a program in C, that will read both “SALES.DAT” and “TAX.DAT” to calculate total sales and total tax to print on the monitor.

Examples Example 1: # include <stdio.h> #include <stdlib.h> void main() { FILE *out_file; int cnt; out_file = fopen(“SALES.DAT”,”w”); randomize(); for (cnt=0; cnt<50; cnt++) fprintf(out_file,“\n%d”,rand()%501); } fclose(out_file); } /* end of main */

Examples Example 2: # include <stdio.h> void main() { int sale_amo; FILE *in_file,*out_file; in_file = fopen(“SALES.DAT”,”r”); out_file = fopen(“TAX.DAT”,”w”); fscanf(in_file,”%d”,&sale_amo); while (sale_amo<>EOF) { fprintf(out_file,“\n%f”,sale_amo * 0.25); } fclose(in_file); fclose(out_file); } /* end of main */

Examples Example 3: # include <stdio.h> void main() { int tot_sales = 0, sale; float tot_tax = 0, tax; FILE *sale_file,*tax_file; sale_file = fopen(“SALES.DAT”,”r”); tax_file = fopen(“TAX.DAT”,”r”); fscanf(sale_file,”%d”,&sale); fscanf(tax_file,”%f”,&tax); while (sale<>EOF) { tot_sale += sale; } /* program is continuing on the next slide */

Examples Example 3: /* The beginning of the program is on the previous slide */ while (tax<>EOF) { fscanf(tax_file,”%f”,&tax); tot_tax += tax; } printf(“\n The total Sales are %d “,tot_sale); printf(“\n The total Taxes are %f “,tot_tax); fclose(sale_file); fclose(tax_file); } /* end of main */