Files Notes
Files Notes
FILE
Why files are needed?
When a program is terminated, the entire data is lost. Storing in a file will preserve your
data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of
the file using few commands in C.
You can easily move your data from one computer to another without any changes.
File I/O:-
Sometimes it is necessary to store the data in a manner that can be later retrieved and displayed
either in a part or in whole. This medium is usually a “file” on the disk. File I/O can be handled
by using different functions.
a) Formatted functions:- The file input function fscanf( ) and the file output function fprintf( )
are called formatted file I/O functions.
b)Unformatted functions:- The input functions like getc( ), getw( ), and fread( ) are called
unformatted file input functions and putc( ), putw( ), and fwrite( ) functions are unformatted file
output functions. Each and every function is having its own syntax and meaning.
File streams:- Stream is either reading or writing of data. The streams are designed to allow the
user to access the files efficiently. A stream is a file or physical device like key board, printer,
monitor, etc., The FILE object uses these devices. When a C program is started, the operating
system is responsible for opening three streams: standard input stream (stdin), standard output
stream (stdout), standard error(stderr).Normally the stdin is connected to the keyboard, the
stdout and stderr are connected to the monitor.
Files
File is a collection of bytes that is stored on secondary storage devices like Hard disk.
OR
A file represents a sequence of bytes on the disk where a group of related data is stored. File is
created for permanent storage of data. It is a ready made structure.
Note:
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text
editors.
When you open those files, you'll see all the contents within the file as plain text. You can easily
edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provide least security and takes
bigger storage space.
2. Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold higher amount of data, are not readable easily and provides a better security than
text files.
File Operations
In C, you can perform four major operations on the file, either text or binary:
Naming a file/Creation of new file
Opening an existing file
Reading data from file
Writing data into file
Closing a file
Steps for processing a file
Declare a file pointer
open a file using fopen() function
Process the file using suitable file functions.
close the file using fclose() function.
Mode Description
Note:
fscanf is used to read list of items from a file
fprintf is used to write a list of items to a file.
Note:
EOF – End of file (when EOF encountered the reading / writing should be terminated)
4 fscanf() To read set of data from file. fscanf(fp, "control string", list)
file1.txt
a
fgetc() function
The fgetc() function returns/read a single character from the file. It gets a character from the
stream. It returns EOF at the end of file.
Example:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
fclose(fp);
getch();
myfile.txt
fputs()
Example:
#include<stdio.h>
void main(){
FILE *fp;
clrscr();
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
getch();
myfile2.txt
hello c programming
fgets()
Example:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
clrscr();
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
Output:
hello c programming
These are integer oriented functions. These are similar to above functions and are used to read
and write integer values. These are useful when we deal with only integer data. The general
format is
Ex: int x = 5;
putw(x,fp);
Ex: int x;
x = getw (fp);
The fseek() function is used to set the file pointer to the specified offset. It is used to write data
into file at desired location.
syntax:
Whence Meaning
SEKK_CUR Starts the offset from the current location of the cursor in the file.
or
Offset is a number or variable of type long, it specifies the number of positions (bytes) to
be moved from the location specified. If offset is positive number, then moving forward
or negative meaning move backwards.
Position is a n integer number and it specifies from which position the file pointer to be
moved. Position can take one of the following three values.
0 beginning of file
1 current position
2 end of file
Eg: fseek (fp, 0L,0); - go to the beginning of the file. (Similar to rewind).
fseek (fp, 0L,1); - Stay at current position (Rarely used)
fseek (fp, 0L,2); -go to the end of the file, past the last character of the file.
Example:
#include <stdio.h>
void main(){
fp = fopen("myfile.txt","w+");
fclose(fp);
myfile.txt
rewind()
This function places the file pointer to the beginning of the file, irrespective of where it is present
right now. It takes file pointer as an argument.
Syntax:
rewind( fp);
Example:
File: file.txt
Example:
#include<stdio.h>
#include<conio.h>
void main(){
char c;
clrscr();
fp=fopen("file.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
while((c=fgetc(fp))!=EOF){
printf("%c",c);
fclose(fp);
getch();
Output:
As you can see, rewind() function moves the file pointer at beginning of the file that is why "this
is simple text" is printed 2 times. If you don't call rewind() function, "this is simple text" will be
printed only once.
ftell()
The ftell() function returns the current file position of the specified stream. We can use ftell()
function to get the total size of a file after moving file pointer at the end of file. We can use
SEEK_END constant to move the file pointer at the end of file.
syntax:
n = ftell(fp);
#include <stdio.h>
#include <conio.h>
FILE *fp;
int length;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fclose(fp);
getch();
Output:
File
handling functions Description
scanf ()
scanf () function reads formatted data from