introduction, file, types of files, need for file handling, steps for processing a file, file input/out functions {declaraion of file, opening a file, reading data from a file, writing data in a file, closing the file}, programs #technology #computers
2. CONTENT-
INTRODUCTION
WHAT IS A FILE?
TYPES OF FILES
NEED OF FILE HANDLING
STEPS FOR PROCESSING A FILE
FILE INPUT/OUTPUT FUNCTIONS
DECLARATION OF A FILE
OPENING OF A FILE
READING DATA FROM A FILE
WRITING DATA IN A FILE
CLOSING THE FILE
PROGRAMS
3. WHAT IS A FILE?
A FILE IS A COLLECTION OF DATA STORED IN ONE UNIT,
IDENTIFIED BY A FILENAME.
IT CAN BE A DOCUMENT, PICTURE, AUDIO OR VIDEO STREAM,
DATA LIBRARY, APPLICATION, OR OTHER COLLECTION OF DATA.
FILES CAN BE OPENED, SAVED, DELETED, AND MOVED TO
DIFFERENT FOLDERS.
THEY CAN ALSO BE TRANSFERRED ACROSS NETWORK
CONNECTIONS OR DOWNLOADED FROM THE INTERNET.
A FILES TYPE CAN BE DETERMINED BY VIEWING THE FILES
ICON OR BY READING THE FILE EXTENSION.
4. INTRODUCTION
FILE HANDLING IS STORING OF DATA IN A FILE USING A
PROGRAM.
WE CAN EXTRACT/ FETCH DATA FROM A FILE TO WORK WITH
IT IN THE PROGRAM.
FILES ARE USED TO STORE DATA IN A STORAGE DEVICE
PERMANENTLY.
FILE HANDLING PROVIDES A MECHANISMTO STORE THE
OUTPUT OF A PROGRAM IN A FILE AND TO PERFORM VARIOUS
OPERATIONS ON IT.
5. TYPES OF FILES
TEXT FILES - IT IS THE DEFAULT MODE OF A FILE.
EACH LINE IN A TEXT FILE IS TERMINATED WITH THE SPECIAL
CHARACTER KNOWN AS END OF LINE. THE EXTENSION OF TEXT
FILE IS .txt.
BINARY FILES – IT CONTAINS SAME FORMAT IN WHICH THE
INFORMATION IS HELD IN THE MEMORY. NO TRANSLATIONS ARE
REQUIRED IN THIS FILE.
CSV FILES – THIS IS THE COMMA SEPARATED VALUES FILE, WHICH
ALLOWS DATA TO BE SAVED IN TABULAR FORM. THESE ARE USE
TEXT FILES BINARY
FILES
CSV FILES
6. need of file handling
A FILE IN ITSELF IS A BUNCH OF BYTES STORED ON
SOME STORAGE DEVICE.
FILE HELPS US TO STORE DATA PERMANENTLY, WHICH
CAN BE RETRIEVED IN FUTURE.
C - LANGUAGE PROVIDES A CONCEPT OF FILE THROUGH
WHICH DATA CAN BE STORED ON A DISK OR
SECONDARY STORAG DEVICE.
THE STORED DATA CAN BE RETRIEVED WHENEVER
NEEDED.
FILE HANDLING IS REQUIREDTO STORE THE OUTPUT OF
THE PREOGRAM WHICH CAN BE USED IN THE FUTURE.
NO MATTER WHATEVER APPLICATION WE DEVELOP , WE
NEED TO STORE THAT DATA SOMEWHERE, AND HERE
DATA HANDLING COMES INTO PLAY.
7. STEPS FOR PROCESSING A
FILE
DECLARE A FILE POINTER VARIABLE
OPEN A FILE USING fopen() function
PROCESS THE FILE USING SUITABLE
FUNCTION
CLOSE THE FILE USING fclose()
function
9. DECLARATION OF A FILE
POINTER
• The type of file that is to be used must
be specified
• This is accomplished by using a
VARIABLE called FILE POINTER (fp)
• Pointer variable that points to a structure FILE
• The members of the FILE structure are used by the
program in various file access operations, but
programmers do not need to be concerned about
them.
• For each file that is to be OPENED, a
pointer type FILE must be declared.
FILE is a structure declared in stdio.h
10. When the function fopen() is called, that function
creates an instance of the FILE structure and
returns a pointer to that structure .
This pointer is used in all subsequent operations
on the file.
The SYNTAX for declaring file pointers is as
follows; FILE *file_pointer_name,…;
FILE *fp;
11. OPENING A FILE: FOR
CREATION AND EDIT
Opening a file is performed using the fopen() function
defined in the stdio.h header file.
The syntax for opening a file in standard I/O is:
Function fopen takes the name of a file as it's 1st
parameter
And mode as it's 2nd parameter.
The string given as first parameter for filename, opens
the file named and assigns an identifier to the FILE type
pointer fp.
This pointer, which contains all the information about
the file is subsequently used as a communication link
between the system and program.
The string given as second parameter for mode,
FILE
*fopen("filename","mode");
12. A filename in a C program can also contain path information
The path specifies the drive and/or directory or folder name
where the file is located.
If a filename is specified without a path, it will be assumed
that the file is located wherever the operating system
currently designates as the default.
On PCs, the backslash character ( ) is used to separate
directory names in a path.
It is to be remembered that the backslash character has a
special meaning to C with respect to escape sequence when it
is in a string. To represent the backslash character itself, one
must precede it with another backslash.
Thus, in a C program, the filename would be represented as
follows.
“c:examdatalist.txt”;
Directory
name
Drive
name
File
name
13. Mode can be one of the following;
If the purpose is " reading" and if it exists, then the file is opened
with current contents safe otherwise an error occurs.
When the mode is " writing", a file with the specialised name is
created, if the file does not exist. The contents are deleted, if the
file already exists.
When the purpose is " appending", the file is opened with the
current contents safe. A file with the specified name is created if
the file does not exist.
14. These statements are used to create a text file with
the name data.dat under current directory
It is opened in "w" mode as data are to be written into
the file data.dat
FILE *fp;
fp =
fopen("data.dat","w");
Following is an example where a file pointer “ fp” is declared, the file
name, which is declared to contain a maximum of 80 characters, is
obtained from the keyboard and then the file is opened in the “write”
mode.
char filename[80];
FILE *fp;
printf(“Enter the filename to be
opened”);
gets(fi lename);
fp = fopen(fi lename,“w”);
15. Writing data In a file
C provides four functions that can be used to
write text file into the disk. These are;
fprintf()
fputs()
fputc()
fwrite()
They are just the file versions of printf() puts()
putc() write()
The only difference is that fprintf() fputs()
fputc() fwrite() expects a pointer to the structure
FILE.
16. EXAMPLE; Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr
fptr = fopen("C:program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
17. Writing to a binary file
To write into a binary file, we need to use the
fwrite() function. The functions take 4
arguments:
1. Address of data to be written in the disk
2. Size of data to be written in the disk
3. Number of such type of data
4. Pointer to the file where you want to write
fwrite(addressData, sizeData, numbersData,
pointerToFile);
19. Reading data from a file
C provides 4 functions that can be used to read text
files from the disk
fscanf()
fgets()
fgetc()
fread()
These are just the file versions of scanf() gets()
getc() and read()
The only difference is that fscanf() fgets() fgetc()
and fread() expects a pointer to the structural FILE.
20. EXAMPLE: Read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:program.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
21. READ FROM A BINARY FILE
Function fread() takes 4 arguments:
1. Address of data to be written in the disk
2. Size of data to be written in the disk
3. Number of such type of data
4. Pointer to the file where you want to read
fread(addressData, sizeData, numbersData,
pointerToFile)
22. Read from a binary file using fread()
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:program.bin","rb")) == NULL){
printf("Error! opening file");
exit(1);
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0:
}
23. Closing a file
The file (both text and binary) should be closed
after readingwriting.
Closing a file is performed using the fclose()
function.
Here, fptr is a file pointer associated with the
file to be closed.
fclose() returns 0 on success or –1 on error
fclose(fptr)
24. When a program terminates
(either by reaching the end of main() or by
executing the exit() function)
All streams are automatically flushed and
closed.
Actually, in a simple program, it is not
necessary to close the file because the system
closes all open files before returning to the
operating system.
25. programs
To open a file, write in it,
and close the file
To open a file, read from
it, and close the file