130 likes | 260 Vues
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.
E N D
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.
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); fscanf(in_file,”%d”,&sale_amo); } 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) { fscanf(sale_file,”%d”,&sale); 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 */