File Operation such as
Reading the file content
Writing the content to the file
Copying the content from one file to another file
Counting the number of character, words and lines of the file
1. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 1
HANDOUT#01
Aim:
File Operation such as
Reading the file content
Writing the content to the file
Copying the content from one file to another file
Counting the number of character, words and lines of the file
Theory:
• 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.
• In C language, we use a structure pointer of file type to declare a file.
FILE *fp;
• 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 existing file
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 a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
2. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 2
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the begining point
• Opening a File or Creating a File
The fopen() function is used to create a new file or to open an existing file.
General Syntax :
*fp = FILE *fopen(const char *filename, const char *mode);
Here 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,
*fp is the FILE pointer (FILE *fp), which will hold the reference to the
opened(or created) file.
mode description
r opens a text file in reading mode
w opens or create a text file in writing mode.
a opens a text file in append mode
r+ opens a text file in both reading and writing mode
w+ opens a text file in both reading and writing mode
a+ opens a text file in both reading and writing mode
rb opens a binary file in reading mode
wb opens or create a binary file in writing mode
3. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 3
ab opens a binary file in append mode
rb+ opens a binary file in both reading and writing mode
wb+ opens a binary file in both reading and writing mode
ab+ opens a binary file in both reading and writing mode
• Closing a File
The fclose() function is used to close an already opened file.
General Syntax :
int fclose( FILE *fp );
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.
• Writinga File
Following is the simplest function to write individual characters to a stream-
int fputc( int c, FILE *fp );
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. You can use the following
functions to write a null-terminated string to a stream –
int fputs( const char *s, FILE *fp );
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. You can use int fprintf(FILE *fp,const char *format,
...)function as well to write a string into a file.
• Reading a File
Given below is the simplest function to read a single character from a file –
4. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
int fgetc( FILE * fp );
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 following function allows to read a string from a stream –
char *fgets( char *buf, int n, FILE *fp );
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.
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.
You can also use int fscanf(FILE *fp, const char *format, ...) function to
read strings from a file, but it stops reading after encountering the first space
character.
[Reference: http://www.tutorialspoint.com/cprogramming/c_file_io.htm and
http://www.studytonight.com/c/file-input-output.php ]
Program:
1. Read the contents of file:
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
FILE *fp;
char ch;
fp=fopen(“doc.txt”,”r”); //doc.txt is already existing file.
if(fp==NULL)
{
puts(“ can not open file”);
exit(1);
}
while(1)
{
ch=getch (fp);
5. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
if(ch=EOF)
{
break;
}
printf(“%c”,ch);
}
fclose(fp);
}
2. Writing into the file:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *fp;
char s[80];
fp=fopen(“poem.txt”,”w”);
if(fp==NULL)
{
puts(“Cannot open the file”);
exit(1);
}
printf(“nEnter a few lines of text:n”);
while(strlen(gets(s))>0)
{
fputs(s,fp);
fputs(“n”,fp);
}
fclose(fp);
}
3. Counting the no. of character, no. of blanks , no. of tabs, no. of lines:
#include<stdio.h>
6. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 6
void main()
{
FILE *fp;
char ch;
int nol=0 ,not=0 ,nob=0, noc=0;
fp=fopen(“file.txt”,”r”);
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
{
break;
}
noc++;
if(ch==’ ’)
{
nob++;
}
if(ch==’n’)
{
nol++;
}
if(ch==’t’)
{
not++;
}
}
fclose(fp);
printf(“nNumber of character: %d”,noc);
printf(“nNumber of blanks: %d”,nob);
printf(“nNumber of tabs: %d”,not);
printf(“nNumber of delete: %d”,nol);
}
4. Copy Program: writing the content of one file into another.
7. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 7
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fs ,*ft;
char ch;
fs=fopen(“poem.txt”,”r”); // poem.txt is already existing file.
if(fs==NULL)
{
puts(“Cannot open the file”);
exit(1);
}
ft=fopen(“new.txt”,”w”); /* new.txt is newly created file in which we
copy the content of poem.txt file. */
if(ft==NULL)
{
puts(“Cannot open the file”);
fclose(fs);
exit(1);
}
while(1)
{
ch=fgetc(fs);
if(ch==EOF)
{
break;
}
else
{
fputc(ch,ft);
}
}
fclose(fs);
fclose(ft);
}
8. System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 8
Input:
Output:
Conclusion:
File Operation such as
Reading the file content
Writing the content to the file
Copying the content from one file to another file
Counting the number of character, words and lines of the file
are implemented in C-language.