File Handling in C language
fclose():
fclose(fp);Ã it writes file buffer data to the
hard disk.
Reading a char from the file:
fgetc() (or) getc()
these are used to
read a character from the file.
Eg:
char ch;
ch=fgetc(fp);Ã to read from the file
ch=getc(fp);
ch=fgetc(stdin); Ã
to read from the keyboard
these two fnctions
returns integer representation of character from the file ( ASCII value )
when file pointer
reaches end of the file returns EOF ( -1 )
EOF is constant
from <stdio.h>
#define EOF -1
getchar() Ã to read a char from input buffer
and returns integer representation of the character.
It is returns -1
when ctrl+d encounterd in the input buffer.
Eg:
char ch;
ch=getchar();
fputc() and
putc() Ã to write a character to the file.
Eg:
char ch=’a’;
fputc(ch,fp);
putc(ch,fp);
program to create
new file:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("c:\\project\\test\\file1","w");
ch=getchar();
while(ch!=EOF)
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
printf("\n
successfully created");
return 0;
}
Program to display
existing file data:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("c:\\project\\test\\file","r");
if(fp==NULL)
{
printf(“\nfile
not found”);
getch();
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
return 0;
}
Assignment:
Program to count number
of characters, words and lines in a
file.
fgetw() anf getw() Ã to read an integer from the file
eg:
int n;
n = fgetw(fp);
n = getw(fp);
fputw() and putw()
à to write integer
to the file.
Eg:
int n=10;
fputw(n,fp) ; putw(n,fp) Ã to write value of variable (n) to the file
fprintf() Ã to write diferrent type of data to
the file.
Eg:
int a=10;
float b=20.5;
char ch =
‘a’;
fprintf(fp,”%d %f %c”,a,b,ch); Ã to write the file
fscanf() Ã to read diferent type of adata
from the file.
Eg:
fscanf( fp, “%d %f %c”,&a,&b,&ch);
fprintf() and fscanf() must follow same sequence of data.
fscanf() Ã
returns number of variables successfully read from the file.
Returns zero ( 0 ) when reaches end of the file.
fwrite() Ã to write structured data to the file.
Syntax:
void fwrite(char *buff, int
rec_size, int rec_count,
FILE *fp);
eg:
struct Employee
{
int
empno;
char ename[15];
float salary;
char
dept[15];
};
struct Employee
emp;
fwrite( &emp,
sizeof(emp), 1, fp);
fread() Ã to read structured data from the
file.
n=fread(&emp,
sizeof(emp), 1, fp);
it return no of
records transferred from file to structure variable.
It returns zero (
0 ), when reaches end of the file.
Below are
pre-defined constants from <stdio.h> header file.
#define SEEK_SET
0
#define
SEEK_CUR 1
#define SEEK_END 2
SEEK_SET Ã
beginig of the file
SEEK_CUR Ã current position
SEEK_END Ã end of the file
fseek()
à to place the file
pointer at required position in a file ( random access )
fseek( fp, 0,
SEEK_SET) Ã to place file
pointer at the beginning of the file.
fseek( fp, n,
SEEK_SET) Ã to place file
pointer at ( n ) bytes from the beginning of the file.
fseek( fp, n,
SEEK_CUR) Ã to place file
pointer ( n ) bytes forward from the current posion of the
file pointer.
fseek( fp, -n,
SEEK_CUR) Ã to place file
pointer ( n ) bytes backward from the current posion of the
file pointer.
fseek( fp, 0,
SEEK_END) Ã to place file
pointer at the end of the file.
fseek( fp, -n,
SEEK_END) to place file pointer ( n
) bytes backward from the end of the
file.
rewind() Ã
to place file pointer at the beginning of the file.
Eg:
rewind(fp);
ftell() Ã
returns current posion of the pointer from the beging of the file.
long int
n;
n = ftell(fp);
remove() Ã to delete a file
int remove( char * file_path);
eg:
int n;
n=remove(“file1”);
on successfully removing
file, it returns 0, if faild to remove,
it returns -1
rename() Ã to re-name a file.
int rename( char
*old_name, char *new_name);
eg:
int n;
n=rename(“file1”,”filex”);
on successfully re-naming
a file, it returns 0, if faild to remove, it returns -1
0 Comments