Files, File Handling Functions and Programs Dr. Srivastav Sir
Files, File Handling Functions and Programs Dr. Srivastav Sir
RAM
Variable
BUFFER Hard Disk
file
file
Variable
Pointer
The basic file operations
• fopen - open a file- specify how its opened
(read/write) and type (binary/text)
• fclose - close an opened file
• fread - read from a file
• fwrite - write to a file
• fseek/fsetpos - move a file pointer to somewhere
in a file.
• ftell/fgetpos - tell you where the file pointer is
located.
File Open
• The file open function (fopen) serves two
purposes:
• It makes the connection between the physical
file and the stream.
• It creates “a program file structure to store the
information” C needs to process the file.
• Syntax: filepointer=fopen(“filename”, “mode”);
File Close
• When we finish with a mode, we need to close
the file before ending the program or
beginning another mode with that same file.
• To close a file, we use fclose and the pointer
variable: fclose(spData);
fread ()
• Declaration:
size_t fread(void *ptr, size_t size, size_t n, FILE
*stream);
Remarks: fread reads a specified number of
equal-sized data items from an input stream into
a block.
ptr = Points to a block into which data is read
size = Length of each item read, in bytes
n = Number of items read
stream = file pointer
fwrite()
• Declaration:
• size_t fwrite(const void *ptr, size_t size, size_t n,
FILE*stream);
• Remarks:
• fwrite appends a specified number of equal-sized data
items to an output file.
• ptr = Pointer to any object; the data written begins at
ptr
• size = Length of each item of data
• n =Number of data items to be appended
• stream = file pointer
How to Write a file
• #include<stdio.h>
• int main()
• {
• int i;
• FILE *fp;
• char s[100];
• fp=fopen("f2.txt","w");
• if(fp==NULL)
• {printf("file cannot open");
• exit(1);
• }
• printf("enter a string");
• gets(s);
• for(i=0;i<strlen(s);i++)
• fputc(s[i],fp);
• getch();
• fclose(fp);
• getch();
• }
How to read a file
• #include <stdio.h>
• int main()
• {
• char ch;
• FILE *fp;
• fp=fopen("f1.txt","r");
• if(fp==NULL)
• {
• printf("file not found");
• exit(1);
• }
• ch=fgetc(fp);
• while(!feof(fp))
• {
• prinf("%c",ch);
• ch=fgetc(fp);
• }
• fclose(fp);
• }
Fputs function
• #include<stdio.h>
• int main()
• {
• char str[20];
• FILE *fp;
• fp=fopen("cse.txt","w");
• printf("enter your name");
• gets(str);
• fputs(str,fp);
• fclose(fp);
• }
• #include<stdio.h>
Fwrite function
• struct book
• {
• int bookid;
• char tittle[20];
• float price;
• };
• void main()
• {
• struct book b1;
• FILE *fp;
• fp=fopen("iiitp.dat","wb");
• printf("enter book id,tittle and price" );
• scanf("%d",&b1.bookid);
• fflush(stdin);
• gets(b1.tittle);
• scanf("%f",&b1.price);
• fwrite(&b1,sizeof(b1),1,fp);
• fclose(fp);
• #include<stdio.h>
Fread function
• struct book
• {
• int bookid;
• char tittle[20];
• float price;
• };
• void main()
• {
• struct book b1;
• FILE *fp;
• fp=fopen("iiitp.dat","rb");
• if(fp=NULL)
• {
• printf("enter book id,tittle and price" );
• exit(1);
• }
• fread(&b1,sizeof(b1),1,fp);
• printf("%d%s%f",b1.bookid,b1.tittle,b1.price);
• fclose(fp);
• }