Programming in C FILE PROCESSING
Programming in C FILE PROCESSING
FILES
A file represents a sequence of bytes on the disk where a group of related data is stored.
File is created for permanent storage of data.
It is a readymade structure.
Why files are needed?
o When a program is terminated, the entire data is lost. Storing in a file will preserve your data even
if the program terminates.
o If you have to enter a large number of data, it will take a lot of time to enter them all.
o However, if you have a file containing all the data, you can easily access the contents of the
file using few commands in C.
o You can easily move your data from one computer to another without any changes.
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit
or delete the contents.
They take minimum effort to maintain, are easily readable, and provide least security and takes
bigger storage space.
1. w(write):
This mode opens new file on the disk for writing.If the file exist,disk for writing.If the file exist, then it will be
over written without then it will be over written without any confirmation.
SYNTAX:
fp=fopen("data.txt","w");
"data.txt" is filename
"w" is write mode.
2. r (read)
This mode opens an preexisting file for reading.If the file doesn‟t Exist then the compiler returns a NULL to the
file pointer
SYNTAX:
fp=fopen("data.txt","r");
3. w+(read and write)
This mode searches for a file if it is found contents are destroyed If the file doesn‟t found a new file is created.
SYNTAX:
fp=fopen("data.txt","w+");
4. a(append)
This mode opens a preexisting file for appending the data.
SYNTAX:
fp=fopen("data.txt",");
5 .a+(append+read)
the end of the file.
SYNTAX:
fp=fopen("data.txt","a+");
6. r+ (read +write)
This mode is used for both Reading and writing
Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold higher amount of data, are not readable easily and provides abetter security than
text files.
1. wb(write)
SYNTAX:
fp=fopen(“data.dat”,”wb”);
2. rb(read)
SYNTAX:
fp=fopen(“data.dat”,”rb”);
3. ab(append)
this opens a binary file in a Append mode i.e. data can be added at the end of file.
SYNTAX:
fp=fopen(“data.dat”,”ab”);
4. r+b(read+write)
SYNTAX:
fp=fopen(“data.dat”,”r+b”);
5. w+b(write+read)
this mode creates a new file for reading and writing in Binary mode.
SYNTAX:
fp=fopen(“data.dat”,”w+b”);
6. a+b(append+write)
this mode opens a file in append mode i.e. data can be written at the end of file.
SYNTAX:
fp=fopen(“data.dat”,”a+b”);
File Operations
In C, you can perform four major operations on the file, either text or binary:
a. Creating a new file
b. Opening an existing file
c. Closing a file
d. Reading from and writing information to afile
e. C provides a number of functions that helps to perform basic file operations. Following
are the functions,
Function Description
fopen() create a new file or open a existingfile
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from afile
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desirepoint
ftell() gives current position in the file
rewind() set the position to the beginningpoint
Opening a File or Creating a File
Thefopen() function is used to create anew fileorto open an existing file.
Syntax:
*fp = FILE fopen(const char *filename, const char *mode);
Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened (or created) file.
Filename is the name of the file to be opened and mode specifies the purpose of opening the file. Mode
can be of following types,
Mode Description
writing mode
Here fclose() function closes the file and returns zero on success, or EOF if there is an error in
closing the file. This EOF is a constant defined in the header file stdio.h.
Example:
#include<stdio.h>
int main()
{
Output:
Enter data...hello welcome to it department
FILE *fp; char ch; The Contents of file is
fp = fopen("one.txt", "w"); hello welcome to it department
printf("Enter data...");
while( (ch = getchar()) != '\n')
{
putc(ch, fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
printf(“The Contents of file is \n”);
while((ch =fgetc(fp))!= EOF)
{
printf("%c",ch);
}
fclose(fp);
return 0;
}
TYPES OF FILE PROCESSING: SEQUENTIAL ACCESS, RANDOM ACCESS
Sequential access
In this type of files data is kept in sequential order if we want to read the last record of the file, we
need to read all records before that record so it takes more time.
Random access
In this type of files data can be read and modified randomly .If we want to read the last record we
can read it directly. It takes less time when compared to sequential file.
There is no need to read each record sequentially, if we want to access a particular
record. C supports these functions for random access file processing.
1. fseek()
2. ftell()
3. rewind()
fseek():
It is used to move the reading control to different positions using fseek function.
Syntax:
Where
Pointer position:
This sets the pointer position in the file.
0 Beginning of file
1 Current position
2 End of file
Example:
1) fseek( p,10L,0)
0 means pointer position is on beginning of the file, from this statement pointer position is skipped
10 bytes from the beginning of thefile.
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(): It tells the byte location of current position of cursor in file pointer.
rewind(): It moves the control to beginning of the file.
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);
This function is used to move the file pointer to the beginning of the given file.
Syntax:
Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which
holds pointers of type char which points to the arguments passed to the program.
Example:
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments suppliedare:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
}
else
{
printf("argument list is empty.\n");
}
return 0;
}
Remember that argv[0] holds the name of the program and
argv[1] points to the first command line argument and argv[n] gives the last argument.