PSPS
UNIT - 4
GMR Institute of Technology
Dr.Balajee Maram,
PhD,ME,MBA,MCJ
Associate Professor/CSE
GMRIT
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
GMR Institute of Technology
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 a few
commands in C.
• You can easily move your data from one computer
to another without any changes.
2
Types of Files
1. Text files
• Text files are the normal .txt files. You can easily
create text files using any simple text editors such
as Notepad.
GMR Institute of Technology
• 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 the least security and takes
bigger storage space.
3
2. Binary files
• Binary files are mostly the .bin files in your
GMR Institute of Technology
computer.
• Instead of storing data in plain text, they
store it in the binary form (0's and 1's).
• They can hold a higher amount of data,
are not readable easily, and provides
better security than text files.
4
GMR Institute of Technology
File opening modes in C
5
As given above, if you want to perform operations on
a binary file, then you have to append ‘b’ at the last.
For example, instead of “w”, you have to use “wb”,
instead of “a+” you have to use “a+b”. For performing
GMR Institute of Technology
the operations on the file, a special pointer called File
pointer is used which is declared as
FILE *filePointer;
So, the file can be opened as
filePointer = fopen(“fileName.txt”, “w”)
6
Functions for file handling
GMR Institute of Technology
7
How to Create a File
Whenever you want to work with a file, the first step is to create a
file. A file is nothing but space in a memory where data is stored.
To create a file in a 'C' program following syntax is used,
FILE *fp;
GMR Institute of Technology
fp = fopen ("file_name", "mode");
In the above syntax, the file is a data structure which is defined in
the standard library. fopen is a standard function which is used to
open a file.
If the file is not present on the system, then it is created and then
opened.
If a file is already present on the system, then it is directly opened
using this function.
fp is a file pointer which points to the type file.
8
Writing to a file
• In C, when you write to a file, newline characters '\n' must be
explicitly added.
The stdio library offers the necessary functions to write to a file:
• fputc(char, file_pointer): It writes a character to the file
GMR Institute of Technology
pointed to by file_pointer.
• fputs(str, file_pointer): It writes a string to the file pointed to
by file_pointer.
• fprintf(file_pointer, str, variable_lists): It prints a string to
the file pointed to by file_pointer. The string can optionally
include format specifiers and a list of variables variable_lists.
9
fputc() Function
#include <stdio.h>
int main() {
int i;
FILE * fptr;
GMR Institute of Technology
char fn[50];
char str[] = "GMRIT is good\n";
fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing mode"
for (i = 0; str[i] != '\n'; i++) {
/* write to file using fputc() function */
fputc(str[i], fptr);
}
fclose(fptr);
return 0;
} 10
fputs () Function
#include <stdio.h>
Void main() {
FILE * fp;
fp = fopen("fputs_test.txt", "w+");
GMR Institute of Technology
fputs("This is Files Tutorial on fputs,", fp);
fputs("We don't need to use for loop\n", fp);
fputs("Easier than fputc function\n", fp);
fclose(fp);
}
11
fprintf()Function
void main (void)
{
FILE *fileName;
fileName = fopen("anything.txt","r");
GMR Institute of Technology
fprintf(fileName, "%s %s %d", "Welcome", "to", 2018);
fclose(fileName);
}
12
Reading data from a File
• fgetc(file_pointer): It returns the next character from the
file pointed to by the file pointer. When the end of the file
has been reached, the EOF is sent back.
GMR Institute of Technology
• fgets(buffer, n, file_pointer): It reads n-1 characters from
the file and stores the string in a buffer in which the NULL
character '\0' is appended as the last character.
• fscanf(file_pointer, conversion_specifiers,
variable_adresses): It is used to parse and analyze data.
It reads characters from the file and assigns the input to a
list of variable pointers variable_adresses using conversion
specifiers. Keep in mind that as with scanf, fscanf stops
reading a string when space or newline is encountered.
13
GMR Institute of Technology
fscanf()
14
GMR Institute of Technology
fgets()
15
GMR Institute of Technology
Getc()
16
GMR Institute of Technology
Fclose()
17
GMR Institute of Technology
18
GMR Institute of Technology
Fwrite()
19
GMR Institute of Technology
Fwrite()
20
GMR Institute of Technology
fread()
21
GMR Institute of Technology
fseek()
22
Example on fseek()
#include <stdio.h>
void main(){
FILE *fp;
fp = fopen("myfile.txt","w+");
GMR Institute of Technology
fputs("This is Ravi", fp);
fseek( fp, 7, SEEK_SET );
fputs(“Rama", fp);
fclose(fp);
}
Output: This is Rama
23