You are on page 1of 15

UNIT V.

FILE MANAGEMENT AND PREPROCESSORS


5.1 File Management:
5.1.1 Defining and Opening a File:
Defining a File:
 A file is defined as FILE structure in the header file “stdio.h”.
 Therefore, all files should be declared as type FILE before they are used.
Syntax:

FILE *filepointer;

Example:
i)FILE *fp;
ii)FILE *f1,*f2;
Opening a File:
 Before we read from a file or write to the file, we must open the file.
 By using , fopen() function we can open the desire file in your program.
Syntax:

filepointer=fopen(“filename”,”mode”);

Here,
filename  Name of the file
mode  r – open the file for reading only
w – open the file for writing only
a – open the file for appending only
r+ - open the file for both reading and writing
a+ - open the file for both reading and appending
w+ - open the file for both writing and reading
Example:
i) FILE *fp;
fp=fopen(“a.txt”,”r”);
 The file a.txt is opened for reading only.
ii) FILE *fp;
fp=fopen(“a.txt”,”w”);
 The file a.txt is opened for writing only.
Program:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
putc(‘a’,fp);
fclose(fp);
}
5.1.2 Closing a File:
 An opened file must be closed after all operations on the file have been completed.
 By using , fclose() function we can close the desire file in your program.
Syntax:

fclose(filepointer);

Example:
FILE *f1,*f2;
f1=fopen(“a.txt”,”r+”);
f2=fopen(“b.txt”,”r+”);
…………….
…………….
…………….
fclose(f1);
fclose(f2);
5.1.3 Input/Output Operations on a File:
 The input/output operations on a file has been done by using the below library
functions which are reside in the header file “stdio.h”.
i) getc()
ii) putc()
iii)getw()
iv)putw()
v) fscanf()
vi)fprintf()
i) getc():
 This function is used to read a single character from a file.
 The reading process stops when EOF is reached.
Syntax:

c=getc(filepointer);

ii) putc():
 This function is used to write a single character into a file.
Syntax:

putc(c , filepointer);

Example:
//for getc() and putc()
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
putc(‘a’,fp);
rewind(fp);
printf(“%c”,getc(fp));
fclose(fp);
}
iii) getw():
 This function is used to read a single integer value from a file.
 The reading process stops when EOF is reached.
Syntax:

c=getw(filepointer);

iv) putw():
 This function is used to write a single integer value into a file.
Syntax:

putw(c , filepointer);

Example:
//for getw() and putw()
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
putc(25,fp);
rewind(fp);
printf(“%d”,getw(fp));
fclose(fp);
}
v)fscanf():
 This function is used to read formatted input to the specified file.
Syntax:

fscanf(filepointer,”controlstring”,&variable1,&variable2……&variablen);

vi)fprintf():
 This function is used to write formatted output to the specified file.
Syntax:

fprintf(filepointer,”controlstring”,variable1,variable2……variablen);

Example:
//for fscanf() and fprintf()
#include<stdio.h>
void main()
{
char str[100];
FILE *fp;
fp=fopen(“a.txt”,”r+”);
fprintf(fp,”%s”,”welcome my students”);
rewind(fp);
fscanf(fp,”%s”,str);
printf(“%s”,str);
fclose(fp);
}

5.1.4 Random Access File:


 Random access files are files in which the user can move the filepointer randomly.
 By using the below library functions we can move filepointer randomly in file.
i)ftell()
ii)rewind()
iii)fseek()
i)ftell():
 ftell() function is used to return the current position of the filepointer in the given
file.
Syntax:
n=ftell(filepointer);

Example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
fprintf(fp,”hai”);
printf(“%d”,ftell(fp));
fclose(fp);
}
ii)rewind():
 rewind() function is used to reset the position of the filepointer to the beginning of
the given file.
Syntax:
rewind(filepointer);

Example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
putw(25,fp);
rewind(fp);
printf(“%d”,getw(fp));
fclose(fp);
}
iii)fseek():
 fseek() function is used to move the filepointer to any desired position within the
given file.
Syntax:
fseek(filepointer,offset,position);

Here,
offset – The number of bytes to be moved from the location specified by the
position.
position - 0-Beginning of file, 1-Current position, 2-End of the file

Example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
fprintf(fp,”%s”,”welcome my students”);
fseek(fp,-2,1);
printf(“%d”,ftell(fp));
fclose(fp);
}

5.1.5 Error handling during I/O operations:


 An error may occur during input / output operations on a file.
 Some of the situations in which the error occurs are as follows.
1. When reading the data beyond the EOF mark.
2. Device overflow (i.e., no space in disk)
3. Not opening of files.
4. Opening a file with an invalid file name.
 The following library functions are used to detect I/O errors in the files.
i)feof()
ii)ferror()
i)feof():
 feof() is used to test whether the end of the file is reached or not.
 This function returns a non-zero integer value if all the data from the specified file
has been read. Otherwise it returns zero.
Syntax:

feof(filepointer);

Example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(“a.txt”,”r+”);
while(!feof(fp))
{
printf(“%c”,getc(fp));
}
fclose(fp);
}

ii)ferror():
 ferror() function is used to report the status of the file specified.
 This function returns a non-zero integer if an error has been detected. Otherwise, it
returns zero.
Syntax:

ferror(filepointer);

Example:
#include<stdio.h>
void main()
{
char a;
FILE *fp;
fp=fopen(“a.txt”,”r+”);
a=getc(fp);
while(ferror(fp))
{
printf(“error”);
}
fclose(fp);
}
5.2 Command Line Arguments:
 Command Line Arguments are arguments that are passed from command line to the
main() function of the program.
Syntax:

void main (int argc, char *argv[])


{
------
------
------
}

Here,
argc- argument counter – Counts the number of arguments on the command
line.
*argv[]-argument vector - A pointer to an arrays of character strings that containing
the arguments, one per string
Syntax:
For running the program

C:\TC\BIN >string1 string2 …… stringn

 when this command is executed the variable argc automatically counts the number
of arguments on the command line.
 The array of pointer variable argv can store the strings such as
argv[0]=program name
argv[1]=string1
argv[2]=string2
.
.
.
argv[n]=stringn
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int args,char *argv[])
{
FILE *fp;
char c;
clrscr();
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("Cannot open file\n");
exit(0);
}
c=fgetc(fp);
while(c!=EOF)
{
printf("%c",c);
c=fgetc(fp);
}
fclose(fp);
getch();
}
5.3 Preprocessor Directives:
 Preprocessor processes the source code before the compilation begins.
 Every preprocessor directive must begin with the character #.
 Preprocessor directives are placed in the source program before the main function.
 Preprocessor directives can be subdivided into
i. Macro substitution
ii. File inclusion.
iii. Compiler controlled directives.
5.3.1 Macro Substitution:
 The #define directive defines an identifier and a constant that is substituted in
place of the identifier name each time it is encountered in the file.
 This identifier is called macroname and the replacement process is known as macro
substitution.
 Macro substitution defines a global symbol in the c program.
 There is no need of a semicolon after the statement having a #define directive.
Classification of Macro Substitution:
i)Simple Macro Substitution
ii)Macro Substitution with Arguments
i)Simple Macro Substitution:
Syntax:

#define MACRONAME constant

Here,
#define – Preprocessor Directive
MACRONAME – Name of the MACRO
constant – The value of the MACRONAME
Example:
#define PI 3.14
Program:
#include <stdio.h>
#define PI 3.14
void main()
{
int radius;
float area, circum;
clrscr();
printf("\nEnter radius of circle: ");
scanf("%d",&radius);
area = PI * radius * radius;
printf("\nArea of circle is: %f",area);
circum = 2 * PI * radius;
printf("\nCircumference of circle is: %f",circum);
getch();
}
ii)Macro Substitution with Arguments:
 Macro can be defined with arguments just like function.
Syntax:

#define MACRONAME(arguments) constant

Example:
#define CUBE(x) (x*x*x)
Program:
#include<stdio.h>
#define CUBE(x) (x*x*x)
void main()
{
int x=2;
printf(“Ans=%d”,CUBE(x));
}
5.3.2 File Inclusion Directive:
 File Inclusion Directive is used to include the content of a file into the source code of
a program.
 File Inclusion Directive is “#include”.
 Whenever the #include directive is encountered, the preprocessor inserts the entire
content of the file into the source code of the program.

Syntax:

#include<filename>
or
#include”filename”

Here,
#include – Preprocessor Directive
filename – Name of the file to be include.
Example:
i)#include<stdio.h>
ii)#include”conio.h”
Program:
#include<stdio.h>
void main()
{
printf(“Raja”);
}
5.3.3 Compiler Control Directives:
 Compiling the selected portion of the program for a particular condition is called
“conditional compilation”.
 This Conditional Compilation has done by using the compiler control directives.
Compiler Control Directives:
i) #ifdef…………..#endif
ii)#ifdef………….#else……….#endif
iii) #if…………..#endif
iv)#if………….#else……….#endif
i) #ifdef…………..#endif:
Syntax:
#ifdef MACRONAME
statement1;
statement2;
statement3;
.
.
.
statementn;
#endif

Here,
 If macroname has been defined in the include section then statements 1 to n will be
compiled otherwise not.
Example:
#include<stdio.h>
#define SRI 10
void main()
{
#ifdef SRI
printf(“1”)
#endif
}
ii)#ifdef………….#else……….#endif
Syntax:

#ifdef MACRONAME
statement1;
statement2;
statement3;
.
.
.
statementn;
#else
statement a;
statement b;
statement c;
.
.
.
statement z;
#endif

Here,
 statements 1 to n will be compiled if the macroname is defined otherwise
statements a to z will be compiled.
Example:
#include<stdio.h>
#define SRI 10
void main()
{
#ifdef SREE
printf(“1”)
#else
printf(“5”)
#endif
}
iii) #if…………..#endif:
Syntax:

#if condition
statement1;
statement2;
statement3;
.
.
.
statementn;
#endif

Here,
 If the condition is true then statements 1 to n will be compiled otherwise not.
Example:
#include<stdio.h>
#define SRI 10
void main()
{
#if SRI>0
printf(“1”)
#endif
}
iv)#if………….#else……….#endif
Syntax:

#if condition
statement1;
statement2;
statement3;
.
.
.
statementn;
#else
statement a;
statement b;
statement c;
.
.
.
statement z;
#endif

Here,
 statements 1 to n will be compiled if the condition is true otherwise statements a to
z will be compiled.
Example:
#include<stdio.h>
#define SRI 10
void main()
{
#ifdef SRI<0
printf(“1”)
#else
printf(“5”)
#endif
}

You might also like