8B) C program to demonstrate the handling of holes in a file [VTU SS&OS]

vtu ss & os 8b
Code:
Explanation:
Aim of this program is to demonstrate handling of holes in the file. If you are too interested in knowing what the hole is, read this stack-overflow article.
We first write 16 bytes of data at the beginning of a file.then move cursor to 48th byte of the file and again we write 16 byte data. Then we demonstrate handling of holes from byte 16-48 using octal dump displaying.


#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>

include all these header files for normal operations.

int main()
{
char buff1[ ]="abcdefghijklmnop ";
char buff2[ ]="ABCDEFGHIJKLMNOP ";

main function.
create two 16 byte values and store it in buff1 and buff2 respectively.

int fd;
fd=open("new.dat",O_WRONLY|O_CREAT,0777);

create a variable and store it with address of pointer to a file.
opening of file is done using open() function with 3 parameters. unlike fopen() of C, this is something different. be cautious about parameters.

  • first parameter is File name.
  • second parameter is access specifications (O_CREAT= create if not exists. O_WRONLY= write only.)
  • third parameter is permissions. (depicted using some numbers, integer).


write(fd,&buff1,16);
lseek(fd,48,SEEK_SET);
write(fd,&buff2,16);
close(fd);

now you write first 16 bytes of data at the beginning of file. using write function as shown above.
write function has three parameters.
  • first parameter is file pointer address
  • second parameter is source content of data to be written. This can be direct values or also variables.
  • third parameter is number of bytes to be written. 16 in this case.

printf("The contents of the file are:\n");
system("od -c new.dat");
exit(0);
}

now you show structure of holes in the file using octal dump od' command. system 'od' is system command. execute it in system using system() function. Then exit the program using exit(0).

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!