FILE CONCEPTS Programs
FILE CONCEPTS Programs
File is a collection of bytes that is stored on secondary storage devices like disk. There are two kinds of files
in a system. They are,
1. Opening/Creating a file
2. Closing a file
3. Reading a file
4. Writing in a file
Let us see the syntax for each of the above operations in a table:
fopen() function is used to open a file to perform operations such as reading, writing etc. In
a C program, we declare a file pointer and use fopen() as below. fopen() function creates a
new file if the mentioned file name does not exist.
fopen() – To FILE *fp;
open a file fp=fopen (“filename”, ”‘mode”);
Where,
fp – file pointer to the data type “FILE”.
filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+
and a+. Please refer below the description for these mode of operations.
Declaration: int fclose(FILE *fp);
fclose() – To
close a file fclose() function closes the file that is being pointed by file pointer fp. In a C program, we
close a file as below.
fclose (fp);
Declaration: char *fgets(char *string, int n, FILE *fp)
fgets function is used to read a file line by line. In a C program, we use fgets function as
fgets() – To read below.
a file fgets (buffer, size, fp);
where,
buffer – buffer to put the data in.
size – size of the buffer
fp – file pointer
fprintf() – To Declaration:
write into a file int fprintf(FILE *fp, const char *format, …);fprintf() function writes string into a file
pointed by fp. In a C program, we write string into a file as below.
fprintf (fp, “some data”); or
fprintf (fp, “text %d”, variable_name);
r – Opens a file in read mode and sets pointer to the first character in the file. It returns null if file
does not exist.
w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are
overwritten.
a – Opens a file in append mode. It returns null if file couldn’t be opened.
r+ – Opens a file for read and write mode and sets pointer to the first character in the file.
w+ – opens a file for read and write mode and sets pointer to the first character in the file.
a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it
can’t modify existing contents.
1. Example program for file open, file write and file close in C language:
C
/ * Open, write and close a file : */
# include <stdio.h>
# include <string.h>
int main( )
{
FILE *fp ;
char data[50];
// opening an existing file
printf( "Opening the file test.c in write mode" ) ;
fp = fopen("test.c", "w") ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ;
return 1;
}
printf( "\n Enter some text from keyboard” \
“ to write in the file test.c" ) ;
// getting input from user
while ( strlen ( gets( data ) ) > 0 )
{
// writing in the file
fputs(data, fp) ;
fputs("\n", fp) ;
}
// closing the file
printf("Closing the file test.c") ;
fclose(fp) ;
return 0;
}
Output:
Opening the file test.c in write mode
Enter some text from keyboard to write in the file test.c
Hai, How are you?
Closing the file test.c
2. Example program for file open, file read and file close in C language:
This file handling C program illustrates how to read the contents of a file. Assume that, a file called “test.c”
contains the following data “Hai, How are you?”. Let’s read this data using following C program.
/* Open, Read and close a file: reading string by string */
# include <stdio.h>
int main( )
{
FILE *fp ;
char data[50] ;
printf( "Opening the file test.c in read mode" ) ;
fp = fopen( "test.c", "r" ) ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ;
return 1;
}
printf( "Reading the file test.c" ) ;
while( fgets ( data, 50, fp ) != NULL )
printf( "%s" , data ) ;
printf("Closing the file test.c") ;
fclose(fp) ;
return 0;
}
Output:
Opening the file test.c in read mode
Reading the file test.c
Hai, How are you?
Closing the file test.c
fclose(f);
}
#include <stdio.h>
main()
{
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
int i;
return 0;
}
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
fclose(fptr);
}
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
Input/Output operation on File
In the above table we have discussed about various file I/O functions to perform reading and writing on file.
getc() and putc() are simplest functions used to read and write individual characters to a file.
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
fclose(fp);
}
fwrite(data-element-to-be-written, size_of_elements,
number_of_elements, pointer-to-file);
fread() is also used in the same way, with the same arguments like fwrite() function. Below mentioned is a
simple example of writing into a binary file
const char *mytext = "The quick brown fox jumps over the lazy dog";
FILE *bfp= fopen("test.txt", "wb");
if (bfp) {
fwrite(mytext, sizeof(char), strlen(mytext), bfp) ;
fclose(bfp) ;
}
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr1, *fptr2, *fptr3;
int n, i, num;
clrscr();
printf("Enter number of values : ");
scanf("%d", &n);
printf("\nEnter the values : ");
fptr1 = fopen("NUMBERS.DAT", "w");
for(i = 0 ; i < n ; i++)
{
scanf("%d", &num);
putw(num, fptr1);
}
fclose(fptr1);
fptr1 = fopen("NUMBERS.DAT", "r");
fptr2 = fopen("ODD.DAT", "w");
fptr3 = fopen("EVEN.DAT", "w");
while((num = getw(fptr1)) != EOF)
{
if(num % 2 == 0){
putw(num, fptr3) ;
} else{
putw(num, fptr2) ;
}
}
fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
fptr2 = fopen("ODD.DAT", "r");
fptr3 = fopen("EVEN.DAT", "r");
printf("\nContents of ODD file is : ");
while((num = getw(fptr2)) != EOF){
printf("%d\t", num) ;
}
printf("\n\nContents of EVEN file is : ") ;
while((num = getw(fptr3)) != EOF){
printf("%d\t", num);
}
fclose(fptr2);
fclose(fptr3);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
FILE *fptr1;
clrscr();
printf("Enter the text to be stored in the file.\n");
printf("Use ^Z or F6 at the end of the text and press ENTER: \n\n");
fptr1 = fopen("COURSES.DAT","w");
while((c = getc(stdin)) != EOF){
fputc(c, fptr1);
}
fclose(fptr1);
printf("\nThe content of the file is : \n\n");
fptr1 = fopen("COURSES.DAT", "r");
do
{
c = fgetc(fptr1);
putchar(c);
} while(c != EOF);
fclose(fptr1);
getch();
}
struct emp{
char name[20];
int age;
float bs;
};
struct emp e;
fp = fopen("EMP.DAT","wb");
if(fp == NULL){
puts("Cannot open file");
}
The following code segment illustrates how to read data from file in binary format.
fp = fopen("EMP.DAT","rb");
while(fread(&e,sizeof(e),1,fp)==1){
printf("%s %d %f\n",e.name,e.age,e.bs);
}
fclose(fp);
void main()
{
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
fclose(fptr);
}
GETS and PUTS – Writing sentence in a text file
This program stores a sentence entered by user in a file.
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}