B.C.A. Semester - II CA-118 Advanced Programming Unit-3 Files What Is A File?
B.C.A. Semester - II CA-118 Advanced Programming Unit-3 Files What Is A File?
Semester - II
Unit-3 Files
What is a File?
File is a collection of bytes that is stored on secondary storage devices like disk.
When a computer reads a file, it copies the file from the storage device to main memory.
When a computer writes to a file, it transfers data from main memory to the storage device.
C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
If you want to keep large volume of data, it is time consuming to enter the entire data.
But, if file is created, this information can be accessed using few commands.
Types of Files
1
File Operations
1. Creating a File
4. Closing a Fie
2
Working with a File
While working with file, you need to declare a pointer of type FILE. This declaration is needed for
communication between file and program.
FILE *file_pointer_name;
Opening a File
For example,
FILE *fp;
fp=fopen("a.txt","w");
Where,
a.txt – the actual file name with full path of the file.
w – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+ and a+.
Example: fopen("E:\\cprogram\\program.txt","w");
3
File Modes:
Closing a File
Syntax: fclose(fp); Here, fp is the file pointer associated with file to be closed.
fcloseall() close all files currently open for reading or writing mode.
4
fprintf()
The function fprintf() is the file version of printf(). The first argument is a pointer to the structure FILE.
Syntax:
FILE *fp;
fp = fopen(“a.txt”,”w”);
#include<stdio.h>
void main()
FILE *fp;
fp=fopen("file1.txt","w");
fprintf(fp,"Hello File");
fclose(fp);
Ouput:
fscanf()
Syntax:
FILE *fp;
fp = fopen(“a.txt”,”r”);
5
#include <stdio.h>
void main()
{
FILE *fptr; // creating a FILE variable
int id, score;
int i, s;
char name[50];
char n[50];
// display detail
printf("\nStudent Details:\n");
printf("\n Name: %s", n);
printf("\n ID: %d", i);
printf("\n Score: %d", s);
printf("\n\nEnd of file.\n");
getc() is a file handling function in C programming language which is used to read a character from a
file.
The getc() will return an end-of-file marker EOF, when end of the file has been reached.
getc(fp) would read a character from the file whose file pointer is fp.
6
putc()
putc() is a file handling function in C programming language which is used to display on standard output
or write into a file.
putc(c, fp) writes the character contained in the character variable c to the file associated with FILE
pointer fp.
#include<stdio.h>
void main()
{
FILE *f1;
char c;
printf("Data Input:\n\n");
printf("\nData Output:\n\n");
getw() is a file handling function in C programming language which is used to read an integer value
from a file.
The getw() will be useful when we deal with only integer data.
7
getw(fp) would read an integer value from the file whose file pointer is fp.
putw()
putw() is a file handling function in C programming language which is used to write an integer value in
a file.
The putw() will be useful when we deal with only integer data.
putw(i, fp) would write an integer value stored in a variable i in a file using file pointer is fp.
#include<stdio.h>
void main()
{
FILE *f;
int n1,n2,no1,no2,sum=0;
f=fopen("sat1.doc","w");
printf("Enter First Number = ");
scanf("%d",&n1);
printf("Enter Second Number = ");
scanf("%d",&n2);
putw(n1,f);
putw(n2,f);
fclose(f);
f=fopen("sat1.doc","r");
no1=getw(f);
no2=getw(f);
sum=no1+no2;
printf("\n The First Number = %d",no1);
printf("\n The Second Number = %d",no2);
printf("\n Summation = %d",sum);
fclose(f);
}
8
Random Access to Files
So far we have seen file functions that are useful for read and write data sequentially. However,
sometime we require to access only a particular part of a file. This can be achieved using fseek() , ftell()
and rewind() functions available in I/O library.
ftell()
Ex. n = ftell(fp);
n would give the relative offset (in bytes) of the current position.
fseek()
fseek() is used to move the file position to a desired location within the file.
The offset specifies the number of positions (bytes) to be moved from the location specified by position.
Value Meaning
0 Beginning of file
1 Current position
2 End of file
If we attempt to move the file pointer beyond the file boundaries, an error occurs and fseek returns -1
(minus one).
9
rewind()
rewind() takes a file pointer and resets the position to the start of the file.
rewind(fp);
n = ftell(fp);
Above two line code would assign 0 to n because the file position has been set to the start of the file by
rewind.
Remember, the first byte in the file is numbered as 0, second as 1, and so on.
Remember, that whenever a file is opened for reading or writing, a rewind is done implicitly.
rename()
The C library function rename() causes the filename referred to by old_filename to be changed
to new_filename.
For renaming a file successfully, it has to be closed, an open file cannot be renamed.
remove()
The C library function remove() deletes the given filename so that it is no longer accessible.
10