2. • A file is a container in computer storage devices
used for storing data
Why files are needed in programming ?
• When a program is terminated, the entire data is
lost.
• Storing in a file will preserve your data even if the
program terminates.
• If we have to enter a large number of data, it will
take a lot of time to enter them all.
• If we have a file containing all the data, we can
easily access the contents of the file .
• Easy to move your data from one computer to
another without any changes
3. Types of Files
• Text files
• Binary files
Text Files
• Text files contain ASCII codes of digits,
alphabetic and symbols.
• Text files are the normal .txt files.
• Create text files using any simple text editors
such as Notepad
4. Binary files
• Binary file contains collection of bytes (0’s and 1’s).
Binary files are compiled version of text files.
• Binary files are mostly the .bin files in computer.
5. File Operations
• Creating a new file
• Opening an existing file
• Closing a file
• Reading from and writing information to a file
6. Working with files
• Need to declare a pointer of type file
FILE *fptr ;
• This declaration is needed for communication
between the file and the program.
7. Opening a file - for creation and edit
• Opening a file is performed using
fopen( ) function
• Function is stdio.h header file
• Syntax for opening a file in standard I/O is:
FILE *ptr ;
ptr = fopen ( “ name of file” , “filemode”);
8. File Opening Modes
• r : opens a text file in reading mode.
• w : opens or creates a text file in
writing mode.
• a : opens a text file in append mode. Data is
added at the end of file
• r+ : opens a text file in both reading and
writing mode. ...
• w+ : opens a text file in both reading and
writing mode.
9. • rb : Open for reading in binary mode.
• wb : Open for writing in binary mode.
• ab : data is added at the end of binary file
• rb+:Open for both reading and writing in
binary mode.
• wb+:Open for both reading and writing in
binary mode.
10. Closing a File
• File (both text and binary) should be closed
after reading/writing.
• Closing a file is performed using
the fclose() function.
• Syntax for closing file
fclose ( ptr );
ptr is a file pointer associated with the file to be
closed.
11. Write to a text file
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
FILE *fptr;
ptr = fopen(“C:ECE.txt","w");
if( ptr == NULL)
{ printf("Error!");
exit(1);
}
13. • This program takes a number from the user
and stores in the file ECE.txt.
• After compilation and executing the program,
we can see a text file ECE.txt created in C drive
of computer.
• When we open the file, we can see the integer
we entered.
•
14. Read from a text file
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
FILE *ptr;
ptr = fopen ("C:ECE.txt“ , "r“ );
if ((ptr) == NULL)
{
printf("Error! opening file");
exit(1);
}
17. fopen()
• Declaration: FILE *fopen (const char *filename, const char
*mode)
• fopen() function is used to open a file to perform operations
such as reading, writing .
• fopen() function creates a new file if the mentioned file name
does not exist.
FILE *fp;
fp=fopen (“filename”, ”‘mode”);
18. fprintf()
int fprintf(FILE *fp, const char *format, …);
• fprintf() function writes string into a file pointed by fp.
In a C program, we write string into a file as below.
fprintf (fp, “some data”);
fprintf (fp, “text %d”, variable_name);
19. fgets()
fgets function is used to read a file line by line.
fgets (buffer, size, fp);
where,
buffer – buffer to put the data in.
size – size of the buffer
fp – file pointer
20. putw( )
int putw( int number, FILE *fp );
• putw function is used to write an integer into
a file.
• In a C program, we can write integer value in a
file as below.
putw(i, fp);
where
i – integer value
fp – file pointer
21. getw()
int getw( FILE *fp );
• getw function reads an integer value from a
file pointed by fp.
• In a C program, we can read integer value
from a file as below.
getw(fp);
22. feof()
• feof functions is used to find the end of a file.
feof(fp);
where
fp – file pointer
23. fscanf()
• fscanf() function is used to read formatted
data from a file.
fscanf (fp, “%d”, &x);
Where, fp is file pointer to the data type “FILE”.
x– Integer variable
25. rewind()
• rewind function is used to move file pointer
position to the beginning of the file.
rewind(fp);
Where fp is FILE pointer
26. fputchar()
• fputchar() function is used to write a character
on standard output/screen
fputchar(char);
where char is a character variable/value.
27. fgetchar( )
• fgetchar() function is used to get/read a
character from keyboard input.
fgetchar();
28. fseek()
• fseek() function is used to move file pointer position to the
given location.
fseek(FILE *fp, long int offset, int whence)
where,
fp – file pointer
offset – Number of bytes/characters to be offset/moved from
whence/the current file pointer position
whence – This is the current file pointer position from where
offset is added . Three constants are used to specify it :
29. Three constants
SEEK_SET – It moves file pointer position to the
beginning of the file.
SEEK_CUR – It moves file pointer position to
given location.
SEEK_END – It moves file pointer position to the
end of file.