You are on page 1of 16

Files

Introduction
A File is a collection of related data stored in an auxiliary storage device Is it possible to retain the value of Need for file sum after program
main( ) { int a = 5, b = 5, sum; sum = a + b; } How to input large amount of data to the program? termination ?

Classifications of files
Text file:
a file of characters it cannot contain integers, floating point numbers or any other data structure to store these data types, they must be converted to their character equivalent formats

Binary file
collection of data stored in the internal format of the computer. data can be an integer / float / char /array or any other structured data except file.

File System Basics


file pointer
a file pointer is a pointer to a structure of type FILE. it points to the information that defines various things about the file, including its name, status, and the current position of the file.

EOF
ASCII value 26 (ctrl z) inserted after the last character in the text file The binary file keep track of the end of file from the number of characters present in the directory entry of the file

FILE Data structure


typedef struct { int level; /* fill/empty level of buffer*/ unsigned flags; /* File status flags */ char fd; /* File descriptor */ unsigned char hold; /* Ungetc char if no uffer */ int bsize; /* Buffer size */ unsigned char *buffer; /* Data transfer buffer */ unsigned char *curp; /* Current active pointer */ unsigned istemp; /* Temporary file indicator*/ short token; /* Used for validity checking*/ }FILE;

Operations on Files
fopen( ) Creates/opens a new file for use fclose( ) Closes a file which has been opened for use. getc( ) Reads a character from the file. putc( ) Writes a character to a file. fprintf( ) Writes a set of data values to a file. fscanf( ) Reads a set of data values from a file. getw( ) Reads an integer from a file. putw( ) Writes an integer to a file.

Defining and opening a file


To perform the operations on file following information must be supplied to operating system
Filename A String of Characters that make up a valid filename. ex:
Input.data

store prog.c text.out Data Structure Structure called FILE is defined in the standard I/O function definitions: <stdio.h>. Purpose What we want to do with the file?

fopen() function
creates a new file for use general format

Mode:
r open the file for reading only w open the file for writing only a open the file for appending (or adding ) data to it

fopen( ) function
File Exist r Contents are safe File does not exist Error

w
a

Contents are erased


Contents are safe

New file will be created


New file will be created

Examples

More on recent compilers


additional modes of operation:
r+ the existing file is opened to the beginning for both reading and writing w+ same as w except both for reading and writing a+ same as a except both for reading and writing

Important note:
we can open and use a number of files at a time ( depends on the system !) if a file fails to open it returns NULL

fclose ()
General format for closing the file
fclose( fp );
..

FILE *fp1, *fp2; fp1 = fopen ( data , r ); fp2 = fopen ( results , w );


. .

fclose(fp1); fclose(fp2);

Input/output operations on files


getc() and putc() function
putc( c, fp1);

Character contained in the character variable c is written to the file associated with FILE pointer fp1
c = getc( fp2 );

Reads a character from the file pointer fp2.

Points to be noted
1. The file pointer moves by one character position for every operation of getc() or putc() 2. getc() will return an end-of-file (EOF) marker when end of the file has been reached. Therefore, the reading should be terminated when EOF is encountered

Problem..
Write a program to store Welcome to BITS in a file called bits.txt. Read the same file and display it on the monitor.

You might also like