Need of files:
Data accepted by the program is stored in the primary or
main memory of computer.
It is the volatile memory, looses its data on completion of
program execution.
The purpose of any application is to store the data
permanently for future usage.
What is file?
File is a collection of data stored on the secondary storage
device with a name.
The data within a file can be read, modified and deleted.
According to the data stored in the file, files are
classified into
- Text
files
- Binary
files
According to the data accessing techniques, files are
classified into
- Sequential
access files
- Random
access files.
Data files in C language:
Every language has its resources to work with the files.
C language provides number of pre-defined functions to
perform file i/o.
All these functions are defined with in the header file
"stdio.h"
The following functions are used to perform file operations
in C language
Functions |
purpose |
fopen() |
To create a
new file or to open an existed file |
fclose() |
To close the
opened file |
fgetc() |
To read a
character from the file |
fputc() |
To write a
character on to the file |
fprintf() |
To write
formatted output on to the file |
fscanf() |
To read
formatted data from the file |
feof() |
To find end
of file |
fwrite() |
To write a
record onto the file |
fread() |
To read a
record from the file |
rewind() |
Sends the
record pointer to the beginning of the file |
fseek() |
Sends the
file pointer to the specified place from whence |
We will discourse all these functions one by one with
suitable examples
fopen():
It is the function used to create a new file or to open an
existed file.
It accepts two arguments that are the name of file and mode
of operation.
It returns the file pointer which has low level details of
file.
We perform all the file operations through file pointer.
Function definition:
|
Mode flags:
Flag |
Usage |
w |
Opens the
text file in write mode |
r |
Opens the
text file in read mode |
a |
Opens the
text file in append mode |
w+ |
Opens the
text file in write /read mode |
r+ |
Opens the
text file in read/write mode |
a+ |
Opens the
text file in append/ read mode |
wb |
Opens the
binary file in write mode |
rb |
Opens the
binary file in read mode |
ab |
Opens the
binary file in append mode |
wb+ |
Opens the
binary file in write/read mode |
rb+ |
Opens the
binary file in read/write mode |
ab+ |
Opens the
binary file in append/read mode |
Example:
fputc():
It is the function used to write a character onto the file
through the file pointer.
It accepts two arguments that is the character to be written
and the file pointer
Function definition:
int fputc(char ch, FILE *p)
{
-----------------------------
-----------------------------
-----------------------------
}
Example:
End of file character: (EOF)
While creating a text file we specify the end of file
character using ctrl+z
The ASCII value of ctrl+z
is -1
"stdio.h" defines a macro EOF with the value -1.
EOF is substituted with -1 by preprocessor.
We can use either EOF or -1 to check end of file while
reading or writing on to the file.
fgetc():
It is the function used to read a character from the file
through file pointer.
It fetches character after character from the file as loop
progresses
It accepts the file pointer as argument and returns a
character
Function definition:
int fgetc(FILE *p)
{
------------------------
------------------------
------------------------
}
Example:
fclose():
Every opened file must be closed after performing any operations
in order to use the file by others.
It is the function used to close the opened file.
It accepts the file pointer as argument and returns either 0
or -1
It returns 0 if file operation is successful otherwise
returns -1
Function definition:
int fclose(FILE *p)
{
---------------------
---------------------
---------------------
}
Example:
fclose(p);
Unformatted text files:
Files with continues text are called unformatted text files.
Unformatted files have words, sentences and paragraphs.
fputc() is used to write onto unformatted file.
fgetc() is used to read from unformatted file.
EOF (-1) is used to find the end of unformatted file
Specification1:
Write a program to read the data from keyboard and write on
to the file "nice1"
Program:
#include<stdio.h>
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen("nice1","w");
while(1)
{
ch=getchar();
if(ch==-1)
break;
fputc(ch,p);
}
printf("1 file
created");
fclose(p);
getch();
}
Execution:
Nice informatics
Kavali
pin: 524201
^Z
1 file created
Note: ^z is typed by using ctrl+z which is the end of file
character in DOS
Program explained:
1. A new file with the name "nice1" is created by
opening the file in write mode ("w") using fopen() function. It
returns the file pointer through which we can write data
2. getchar() function reads the data from the keyboard
character by character as loop progresses.
3. fputc() function writes the character on to the file
using file pointer.
4. The if(ch==-1) checks for end of file character, breaks
if it is reached.
fclose() is used to close the file after completion of write
operation.
Specification2:
Write a program to read the data from the file
"nice1" and publish onto the monitor.
Program:
#include<stdio.h>
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen("nice1","r");
while(1)
{
ch=fgetc(p);
if(ch==-1)
break;
printf("%c",ch);
}
fclose(p);
getch();
}
Output:
Nice informatics
Kavali
pin: 524201
Program explained:
1. The file "nice1" is opened in read mode
("r") using fopen() function.
2. fgetc() fetches character by character through the file
pointer.
3. Characters are printed onto the monitor using printf()
4. The if(ch==-1) checks for end of file character, breaks
if it is reached.
5. The file is closed after the completion of read
operation.
0 comments:
Post a Comment