Unit-IV_Files
Unit-IV_Files
Files
A file can be classified into two types based on the way the file
stores the data. They are as follows:
• Text Files
• Binary Files
The binary files store info and data in the binary format of 0’s
and 1’s (the binary number system). Thus, the files occupy
comparatively lesser space in the storage. In simpler words, the
binary files store data and info the same way a computer holds
the info in its memory. Thus, it can be accessed very easily as
compared to a text file.
The binary files are created with the extension .bin in a
program, and it overcomes the drawback of the text files in a
program since humans can’t read it; only machines can. Thus,
the information becomes much more secure. Thus, binary files
are safest in terms of storing data files in a C program.
#include <stdio.h>
void main()
{
FILE *fptr;
fptr = fopen(“abc.txt", "w"); //opening file
fprintf(fptr, "Hello, Welcome to Medicaps.\n"); //writing data into file
fclose(fptr); //closing file
}
To put the file pointer to a specific place, we use fseek(). With the
help of it, we can write data at whatever location we want in the
file.
Syntax -
fseek(FILE stream, long int offset, int whence)
file stream - pointer to the file. As in the example file, the pointer to
the file object "myfile.txt".
offset - This is the number of bytes that must be offset/skipped from
whence.
Hence take three values.
SEEK_SET - sets the pointer at the beginning of the
file. SEEK_CUR - sets the pointer at the current position of the
file SEEK_END - sets the pointer at the end of the file.
Priyanka Parmar Medicaps University,Indore
Program
#include <stdio.h>
int main(){
FILE *file;
file = fopen("myfile.txt","w+");
fputs("Welcome to Medicaps” , file);
fseek( file, 10, SEEK_SET );
fputs(" Computer Apllications !", file);
printf("%s",file);
fclose(file);
}