Unit V
Unit V
UNIT V
FILE PROCESSING Files – Types of file processing: Sequential access, Random access –
Sequential access file - Example Program: Finding average of numbers stored in sequential
access file - Random access file - Example Program: Transaction processing using random
access files – Command line arguments
5.1 FILES
Most of the programs we have seen so far are transient in the sense
that they run for a short time and produce some output, but when they end, their
data disappears. If you run the program again, it starts with a clean slate.
Other programs are persistent: they run for a long time, they keep at least some of
their data in permanent storage, and if they shut down and restart, they pick up
where they left off.
TPGIT/CSE 1
CS8251 Dept of CSE Programming in 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. Hence a binary file is
also referred to as a character stream, but there are two essential differences.
1. No special processing of the data occurs and each byte of data is
transferred to or from the disk unprocessed.
2. 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.
Binary files can be either processed sequentially or, depending on the
needs of the application, they can be processed using random access techniques. In C
Programming Language, processing a file using random access techniques involves
moving the current file position to an appropriate place in the file before reading or
writing data. This indicates a second characteristic of binary files. They a generally
processed using read and writes operations simultaneously. For example, a database
file will be created and processed as a binary file. A record update operation will
involve locating the appropriate record, reading the record into memory, modifying
it in some way, and finally writing the record back to disk at its appropriate location
in the file. These kinds of operations are common to many binary files, but are rarely
found in applications that process text files.
FILE *fptr;
Opening a file
File can be opened with the help of fopen() function available in
stdio.h library file of C
The syntax for opening a file is
TPGIT/CSE 2
CS8251 Dept of CSE Programming in C
r Open for reading. - If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. - If the file does not exist, fopen() returns
NULL.
w Open for writing. - If the file exists, its contents are overwritten. If the file
does not exist, it will be created.
wb Open for writing in binary mode. - If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
a Open for append. i.e, Data is added to end of file. - If the file does not exists,
it will be created.
ab Open for append in binary mode. i.e, Data is added to end of file. - If the
file does not exists, it will be created.
r+ Open for both reading and writing. - If the file does not exist, fopen()
returns NULL.
rb+ Open for both reading and writing in binary mode. - If the file does not
exist, fopen() returns NULL.
w+ Open for both reading and writing. - If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
wb+ Open for both reading and writing in binary mode. - If the file exists, its
contents are overwritten. If the file does not exist, it will be created.
a+ Open for both reading and appending. - If the file does not exists, it will be
created.
ab+ Open for both reading and appending in binary mode. - If the file does not
exists, it will be created.
Example:
fptr=fopen("C:\\TURBOC3\\program.txt","w");
Closing a File
File should be closed after its usage. File can be closed using fclose()
liberary function
fclose(file_pointer);
Here, file_pointer is the one which was created when the file was
opened using fopen() function.
Example:
fclose(fptr);
In order to manipulate files we have to learn about File I/O i.e. how to
write data into a file and how to read data from a file. To read and write file we use
the functions fprintf() and fscanf().
TPGIT/CSE 3
CS8251 Dept of CSE Programming in C
Functions fprintf() and fscanf() are the file version of printf() and
fscanf(). The only difference while using fprintf() and fscanf() is that, the first
argument is a pointer to FILE.
#include <stdio.h>
void main()
{
int n;
FILE *fptr;
fptr=fopen("C:\\TURBOC3\\sample.txt","w");
if(fptr==NULL){
printf("Cannot Open File!");
exit(1);
}
printf("Enter a Number: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
}
This program read a number from user and stores in file. After you
compile and run this program, you can see a text file sample.txt created in
C:\\TURBOC3\\ path of your computer. When you open that file, you can see the
integer you entered. Similarly, fscanf() can be used to read data from file.
#include <stdio.h>
void main()
{
int n;
FILE *fptr;
if
((fptr=fopen("C:\\TURBOC3\\sample.txt","r"))==NULL){printf(
"Cannot Open File ");
exit(1);
}
fscanf(fptr,"%d",&n);
printf("Value in file is=%d",n);
fclose(fptr);
TPGIT/CSE 4
CS8251 Dept of CSE Programming in C
This program reads the integer present in the sample.txt file and prints it onto the
screen.
Other unformatted I/O functions like fgetc(), fputc() , etc.. are used to read and write
data in files.
fputc(), fputs()
The function fputc() writes the character value of the argument c to the
output stream referenced by fp. It returns the written character written on success
otherwise EOF if there is an error.
The function fputs() writes the string s to the output stream referenced by fp. It
returns a non-negative value on success, otherwise EOF is returned in case of any
error.
Example:
#include <stdio.h>
void main() {
FILE *fptr;
fptr = fopen("C:\\TURBOC3\\sample.txt", "w+");
fputs("Printing Using fputs", fptr);
fclose(fptr);
}
When the above program run, it creates a new file sample.txt in C:\\TURBOC3\\
directory and writes the string "Printing Using fputs" to that file.
fgetc(),fgets()
The fgetc() function reads a character from the input file referenced by
fp. The return value is the character read, or in case of any error, it returns EOF.
The functions fgets() reads up to n-1 characters from the input stream referenced by
fp. It copies the read string into the buffer buf, appending a null character to
terminate the string.
TPGIT/CSE 5
CS8251 Dept of CSE Programming in C
If this function encounters a newline character '\n' or the end of the file EOF before
they have read the maximum number of characters, then it returns only the
characters read up to that point including the new line character.
#include <stdio.h>
void main()
{
FILE *fptr;
char buff[255];
fptr = fopen("C:\\TURBOC3\\sample.txt", "r");
fgets(buff, 255, fptr);
printf("%s\n", buff );
fclose(fptr);
}
When the above program run, it reads the file created in the previous program and
produces the following result.
Opening modes of binary files are rb, rb+, wb, wb+,ab and ab+. The
only difference between opening modes of text and binary files is that, b is appended
to indicate that, it is binary file.
Functions fread() and fwrite() are used for reading from and writing to
a file on the disk respectively in case of binary files.
fwrite()
Syntax:
TPGIT/CSE 6
CS8251 Dept of CSE Programming in C
fwrite(address_data,size_data,numbers_data,pointer_to_file);
Example :
#include <stdio.h>
struct marks
{
int m1, m2,m3,m4,m5;
};
void main()
{
int n;
struct marks m;
FILE *fptr;
if ((fptr = fopen("C:\\TURBOC3\\mark.bin","wb")) ==
NULL){printf("File Cannot Open!");
exit(1);
}
printf("Enter 5 Students Marks\n");
for(n = 1; n <= 5; ++n)
{
printf("Enter English Mark of Student %d : ", n);
scanf("%d",&m.m1);
printf("Enter Math's Mark of Student %d : ", n);
scanf("%d",&m.m2);
printf("Enter Physics Mark of Student %d : ", n);
scanf("%d",&m.m3);
printf("Enter Chemistry Mark of Student %d : ", n);
scanf("%d",&m.m4);
printf("Enter Python Mark of Student %d : ", n);
scanf("%d",&m.m5);
fwrite(&m, sizeof(struct marks), 1, fptr);
}
fclose(fptr);
}
Output:
TPGIT/CSE 7
CS8251 Dept of CSE Programming in C
We declare a structure marks with five integers – m1, m2, m3, m4 and m5, and
define it in the main function as m.
Now, inside the for loop, we read marks of 5 subjects and store the value into the file
using fwrite.
The first parameter takes the address of m and the second parameter takes the size of
the structure marks.
Since, we're only inserting one instance of m, the third parameter is 1. And, the last
parameter *fptr points to the file we're storing the data.
fread()
Syntax:
fread(address_data,size_data,numbers_data,pointer_to_file);
Example:
#include <stdio.h>
struct marks
{
int m1, m2,m3,m4,m5;
};
void main()
{
int n;
TPGIT/CSE 8
CS8251 Dept of CSE Programming in C
struct marks m;
FILE *fptr;
if ((fptr = fopen("C:\\TURBOC3\\mark.bin","rb")) ==
NULL){printf("Cannot Open File !");
exit(1);
}
printf("Marks are\n");
for(n = 1; n <= 5; ++n)
{
fread(&m, sizeof(struct marks), 1, fptr);
printf("Student %d Marks : English: %d\t Maths : %d\t Physics: %d\t Chemistry : %d\t
Python: %d\n",n, m.m1, m.m2, m.m3,m.m4,m.m5);
}
fclose(fptr);
}
Output:
In this program, you read the same file mark.bin in C:\\TURBOC3\\ directory and
loop through the records one by one.
In simple terms, you read one marks record of marks size from the file pointed
by *fptr into the structure m.
In computer programming, the two main types of file access are allowed they are:
Sequential
Random access
TPGIT/CSE 9
CS8251 Common to CSE & IT Programming in C
Sequential Access Files: are generally used in cases where the program
processes the data in a sequential fashion – i.e. counting words in a text file. All the
programs explained before are use the sequential access of file.
Random Access Files: There are situation at which the records in the file need
to be accessed randomly. If we want to access a particular record randomly, C
provides these functions for random access file processing.
fseek()
ftell()
rewind()
fseek():
This function is used for seeking the pointer position in the file at the
specified byte.
Syntax:
Pointer position:
This sets the pointer position in the file.
Value pointer position
0 Beginning of file.
1 Current position
2 End of file
Example:
1) fseek( p,10L,0)
2)fseek( p,5L,1)
1 means current position of the pointer position.From this statement pointer position
is skipped 5 bytes forward from the current position.
3)fseek(p,-5L,1)
From this statement pointer position is skipped 5 bytes backward from the current
position.
ftell()
This function returns the value of the current pointer position in the file.The value is
count from the beginning of the file.
Syntax:
ftell(fptr);
rewind()
This function is used to move the file pointer to the beginning of the given file.
Syntax:
rewind( fptr);
Example
Program to read last ‘n’ characters of the file using appropriate file functions (Here
we using fseek() and fgetc()).
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int n;
clrscr();
fp=fopen("C:\\TURBOC3\\fseek.txt", "r");
if(fp==NULL)
printf("file cannot be opened");
else
{
printf("Enter value of n to read last n characters :");
scanf("%d",&n);
fseek(fp,-n,2);
while((ch=fgetc(fp))!=EOF)
{
printf("%c ",ch);
}
}
fclose(fp);
getch();
}
Output:
fseek.txt – file content is
This function is used for seeking the pointer position in the file at the specified byte.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int n;
clrscr();
fp=fopen("C:\\TURBOC3\\fseek.txt", "r");
if(fp==NULL)
printf("file cannot be opened");
else
{
printf("Enter value of n to read last n characters :");
scanf("%d",&n);
fseek(fp,-n,2);
while((ch=fgetc(fp))!=EOF)
{
printf("%c ",ch);
}
}
fclose(fp);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
struct account
{
int number;
long amount;
char name[20];
};
void create()
{
FILE *fptr;
int i, n;
struct account acc;
if ((fptr = fopen("C:\\TURBOC3\\account.bin","wb")) ==
NULL){printf("File Cannot Open!");
exit(1);
}
printf("Enter Total Number of Customers\n");
scanf("%d",&n);
for(i= 1; i <= n; i++)
{
acc.number=i;
}
fclose(fptr);
}
void transfer()
{
int fromno,tono;
long tamount;
struct account acc,fromacc,toacc;
FILE *fptr;
if ((fptr = fopen("C:\\TURBOC3\\account.bin","rb+")) ==
NULL){printf("File Cannot Open!");
exit(1);
}
scanf("%ld",&tamount);
fseek(fptr,(fromno-1)*sizeof(struct account),0);
fread(&fromacc, sizeof(struct account), 1, fptr);
fseek(fptr,(tono-1)*sizeof(struct account),0);
fread(&toacc, sizeof(struct account), 1, fptr);
fromacc.amount-=tamount;
toacc.amount+=tamount;
fseek(fptr,(fromno-1)*sizeof(struct account),0);
fwrite(&fromacc, sizeof(struct account), 1, fptr);
fseek(fptr,(tono-1)*sizeof(struct account),0);
fwrite(&toacc, sizeof(struct account), 1, fptr);
fclose(fptr);
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n MENU \n");
printf("1.Create Account\n2.View Account Detail \n3.Transfer Amount\n4.Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
view();
break;
case 3:
transfer();break;
case 4: exit(0);
}
}
Output:
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("One argument passed. Argument is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("More than one arguments passed.\n");
}
else {
printf("No argument Passed.\n");
}
}
When the above code is compiled and executed with single argument, it produces
the following result.
When the above code is compiled and executed with a two arguments, it produces
the following result.
When the above code is compiled and executed without passing any argument, it
produces the following result.
It should be noted that argv[0] holds the name of the program itself
and argv[1] is a pointer to the first command line argument supplied, and *argv[n]
is the last argument. If no arguments are supplied, argc will be one, and if you pass
one argument then argc is set at 2.
You pass all the command line arguments separated by a space, but if
argument itself has a space then you can pass such arguments by putting them
inside double quotes "" or single quotes ''.
C Program to Copy Content from One File to another File Using Command Line Argument:
Program:
#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{
FILE *sptr,*dptr;
char ch;
if(argc!=3)
{
printf("Command Line Error. Need Two argument");
return;
}
sptr=fopen(argv[1],"r");
dptr=fopen(argv[2],"w");
if(sptr==NULL || dptr==NULL)
{
Output: