0% found this document useful (0 votes)
99 views

Files, File Handling Functions and Programs Dr. Srivastav Sir

A file is a collection of related data that is treated as a single unit by a computer. Files are stored in secondary storage so their contents are preserved when the computer shuts down. When a file is read, its contents are copied from secondary storage to memory, and when written to, data is transferred from memory to secondary storage. The basic file operations are opening, closing, reading from, writing to, and moving the file pointer within a file. Files are opened using fopen(), closed with fclose(), read from using fread(), written to using fwrite(), and the file pointer is moved using fseek()/fsetpos() and its position checked with ftell()/fgetpos().

Uploaded by

RUTUJA MADHURE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Files, File Handling Functions and Programs Dr. Srivastav Sir

A file is a collection of related data that is treated as a single unit by a computer. Files are stored in secondary storage so their contents are preserved when the computer shuts down. When a file is read, its contents are copied from secondary storage to memory, and when written to, data is transferred from memory to secondary storage. The basic file operations are opening, closing, reading from, writing to, and moving the file pointer within a file. Files are opened using fopen(), closed with fclose(), read from using fread(), written to using fwrite(), and the file pointer is moved using fseek()/fsetpos() and its position checked with ftell()/fgetpos().

Uploaded by

RUTUJA MADHURE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

What is a File?

• A file is a collection of related data that a


computers treats as a single unit
• Computers store files to secondary storage so
that the contents of files remain intact when a
computer shuts down
• When a computer reads a file, it copies the file
from the storage device to memory; when it
writes to a file, it transfers data from memory
to the storage device.
Buffer used in file handling

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);
• }

You might also like