Anda di halaman 1dari 9

Notes on : File Handling and Programs M.Sc (IT):Mr.

Subash siwa

WHAT IS FILE?
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,
Text files (ASCII)
Binary files
Text files contain ASCII codes of digits, alphabetic and symbols.
Binary file contains collection of bytes (0s and 1s). Binary files are compiled version of text files.

BASIC FILE OPERATIONS IN C PROGRAMMING:


There are 4 basic operations that can be performed on any files in C programming language. They are,
Opening/Creating a file
Closing a file
Reading a file

MODE OF OPERATIONS PERFORMED ON A FILE IN C LANGUAGE:

There are many modes in opening a file. Based on the mode of file, it can be opened for reading or
writing or appending the texts. They are listed below.
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 couldnt 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 cant
modify existing contents.
1.Formatted input and output function

a)fprintf():Steps for writing the file


1.create a file pointer
2.open file in write(w or a) mode
3.perform general i/ooperation
4.save i/o results using following syntax:
5. fprintf(file_pointer,format specifier,variable list );
6.close the file using fclose(file_pointer)
Types of fprintf()
a) fwrite() function
The fwrite() function is used to write records (sequence of bytes) to the file. A record may be an array or a
structure.
Syntax of fwrite() function

fwrite( ptr, int size, int n, FILE *fp );

The fwrite() function takes four arguments.


ptr : ptr is the reference of an array or a structure stored in memory.
size : size is the total number of bytes to be written.
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

n : n is number of times a record will be written.


FILE* : FILE* is a file where the records will be written in binary mode.
b) fread() function
The fread() function is used to read bytes form the file.
Syntax of fread() function

fread( ptr, int size, int n, FILE *fp );

The fread() function takes four arguments.


ptr : ptr is the reference of an array or a structure where data will be stored after reading.
size : size is the total number of bytes to be read from file.
n : n is number of times a record will be read.
FILE* : FILE* is a file where the records will be read.
//WAP TO SHOW THE FREAD() AND FWRITE()
#include<stdio.h>
#include<conio.h>
struct person
{
int roll;
char name[25];
float mark;
};
main()
{
struct person std;
int n,i;
FILE *fp;
fp=fopen("student.txt","wb");
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
fp=fopen("student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

Output:

b)fscanf():Steps for reading the file


1.create a file pointer
2.open file in read(r) mode
3.read i/o results using following syntax:
fscanf(file_pointer,format specifier,&variable list );
4.display data as required.
5.close the file using fclose(file_pointer)
4.fclose() To close a file
Declaration: int fclose(FILE *fp);
fclose() function closes the file that is being pointed by file pointer fp.
Program:[ fopen(),fclose(),fprintf()]
#include<stdio.h>
main()
{
FILE*fp;
int age;
char name[20];
fp=fopen("first.txt","w");
printf("enter the name\n");
gets(name);
printf("enter the age\n");
scanf("%d",&age);
fprintf(fp,"%s\n%d",name,age);
fclose(fp);

}
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

Output:

3.fscanf()
Steps for reading the file
1.create a file pointer
2.open file in read(r) mode
3.read i/o results using following syntax:
fscanf(file_pointer,format specifier,&variable list );
4.display data as required.
5.close the file using fclose(file_pointer)
4.fclose() To close a file
Declaration: int fclose(FILE *fp);
fclose() function closes the file that is being pointed by file pointer fp.

Program:[ fopen(),fclose(),fscanf()]
#include<stdio.h>
main()
{
FILE*fp;
int age;
char name[20];
fp=fopen("first.txt","r");
fscanf(fp,"%s\n%d",name,&age);
printf("name=%s\n",name);
printf("age=%d",age);
fclose(fp);
}
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

Output:

4.Character input and output function


a)getc():
Declaration: syntax:char variable=getc(file pointer);
Example:ch=getc(fp);
getc ()=getc () function reads character from file.
b)putc():
Declaration: syntax:putc(char variable,file pointer);
Example:putc(ch,fp);
putc ()=putc () function writes a character to file.
c)getchar():
Declaration: syntax:char variable=getchar();
Example:ch=getchar();
getchar ()=getchar () function reads character from keyboard.

//WAP TO SHOW THE FUNCTION OF GETC() AND PUTC()


#include<stdio.h>
main()
{
FILE*fp;
char ch;
fp=fopen("subash.txt","w");
printf("enter the data");
while((ch=getchar()) !=EOF)
{
putc(ch,fp);
}
fclose(fp);
fp=fopen("subash.txt","r");
while((ch=getc(fp)) !=EOF)
{
printf("%c",ch);
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

}
fclose(fp);
Output:

5. String input and output function:


a)fgets():
Declaration: syntax:fgets(char string variable,no. of max character in a string,file pointer );
Example:fgets(ch,17,fp);
fgets ()=fgets () function reads string from a file, one line at a time.
b)fputs():
Declaration: syntax:fputs(char string variable, file pointer );
Example:fputs(ch,fp);
fputs ()=fputs () function writes string to a file.
c)gets():
Declaration: syntax:gets(char string);
Example:gets(ch);
gets ()=gets () function reads line from keyboard.
b)puts():
Declaration: syntax:puts(char variable);
Example:puts(ch);
puts()=puts () function writes line to output screen.
//WAP TO SHOW THE FUNCTION FGETS() AND FPUTS()
#include<stdio.h>
main()
{
FILE*fp;
char ch[30];
fp=fopen("subash.txt","w");
printf("enter the data\n");
gets(ch);
fputs(ch,fp);
fclose(fp);
fp=fopen("subash.txt","r");
printf("output the data\n");
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

fgets(ch,17,fp);
puts(ch);
fclose(fp);
}Output:

6.Word input/Output function : putw(), getw() functions in C


putw(), getw() functions are file handling function in C programming language which is
used to write an integer value into a file (putw) and read integer value from a file (getw).
a)getw():
Declaration: syntax:integer variable=getw(file pointer);
Example:no=getw(fp);
getw ()=getw () function reads integer from file.
b)putw():
Declaration: syntax:putw(integer variable,file pointer);
Example:putw(i, fp);
putw()=putw () function writes integer to file.
c)EOF(): feof() function is a file handling function in C programming language
which is used to find the end of a file.
Syntax: EOF(FILE *fp)
Example:EOF(fp);

//WAP TO SHOW GETW() AND PUTW()


#include<stdio.h>
main()
{
FILE *fp;
int i,no;
fp = fopen("number1.txt", "w");
for(i=1;i<=5;i++)
{
putw(i,fp);
}
fclose(fp);
fp = fopen("number1.txt", "r");
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

printf(" numbers are\n");


while((no=getw(fp))!=EOF)
{

printf("%d\t",no);
}
fclose(fp);
}
Output:

7. Remove() and Rename():


a)Syntax: rename ( const char * oldname, const char * newname );
b)Syntax: remove ( const char * filename );
//WAP TO SHOW RENAME() AND REMOVE()
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fptr,*fp;
char name[20];
int age;
float salary;
fptr=fopen("evanasushant.txt","w");
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
rename("evanasushant.txt","subash2.txt");
remove("evanasushant.txt");
Notes on : File Handling and Programs M.Sc (IT):Mr.Subash siwa

Output:

Best of luck for ur exam


2074
Ss n family

Anda mungkin juga menyukai