C File Handling

A file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. There are two kinds of files that programmers deal with text files and binary files.

C Text Files

A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.

C Binary Files

A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed. C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.

Opening a File in C

The general format of the function used for opening a file is

FILE *fp;

The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. fopen() contain the file name and mode (the purpose of opening the file).

  1. r is used to open the file for read only.
  2. w is used to open the file for writing only.
  3. a is used to open the file for appending data to it.

Closing a File in C

A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer. The input output library supports the function to close a file.



void main(void)

FILE *myfile;
char c;
myfile = fopen(“firstfile.txt”, “r”);
if (myfile == NULL)
printf(“File doesn’t exist\n”);
else {

do {

c = getc(myfile);

} while (c != EOF);



C File Operation Functions

Function Name
fopen() Creates a new file. Opens an existing file.
fclose Closes a file which has been opened for use
getc() Reads a character from a 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 a integer from a file
putw() Writes an integer to the file
fseek() Sets the position to a desired point in the file
ftell() Gives the current position in the file
rewind() Sets the position to the beginning of the file

C Graphics

In a C program first of all you need to initialize the graphics drivers on the computer. This is done using the initgraph method provided in graphics.h library.

Graphics mode Initialization

initgraph() function is used to load the graphics drivers and initialize the graphics system. For every function, that uses graphics mode, graphics mode must be initialized before using that function.

void far initgraph(int far *driver, int far *mode, char far *path)

Function detectgraph

Detectgraph function determines the graphics hardware in the system, if the function finds a graphics adapter then it returns the highest graphics mode that the adapter supports.

void far detectgraph(int far *driver, int far *mode)

Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type.

C Graphic Mode

Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.

Function closegraph

This function shutdown the graphics mode and returns to the position it was before the initgraph function was called.

void far closegraph(void)

C Graphics Example 1

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()

int gd=DETECT, gm;

initgraph(&gd, &gm, “c:\\turboc\\bgi”);



C Graphics Example 2

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()

int gd=DETECT, gm;
initgraph(&gd, &gm, ” c:\\turboc\\bgi”);

outtextxy(75,170, “Circle”);
outtextxy(240, 170, “Rectangle”);
ellipse(500, 100,0,360, 100,50);
outtextxy(480, 170, “Ellipse”);



Useful graphics.h Functions

int getdisplaycolor( int color );
int converttorgb( int color );
void delay( int msec );
void getarccoords( arccoordstype *arccoords );
int getbkcolor( );
int getcolor( );
int getmaxcolor( );
int getmaxheight( );
int getmaxwidth( );
int getmaxx( );
int getmaxy( );
void getfillpattern( char *pattern );
void getfillsettings( fillsettingstype *fillinfo );
void getlinesettings( linesettingstype *lineinfo );
bool getrefreshingbgi( );
int getwindowheight( );
int getwindowwidth( );
int getpixel( int x, int y );
void getviewsettings( viewporttype *viewport );
int getx( );
int gety( );
void setcolor( int color );
void setfillpattern( char *upattern, int color );
void setfillstyle( int pattern, int color );
void setlinestyle( int linestyle, unsigned upattern, int thickness );
void setrefreshingbgi(bool value);
void setviewport( int left, int top, int right, int bottom, int clip );
void setwritemode( int mode );
void moverel( int dx, int dy );
void moveto( int x, int y );
void refreshbgi(int left, int top, int right, int bottom);
void refreshallbgi( );