Hi Friends,
This is my C program to check the type of a file in UNIX system. Before going to program, have a look on what actually a 'type' means in UNIX system. A file may be one of the following types :
- Regular File : It may be either a text file or a binary file.
- Directory File : These are the files that can store other files (like file folder we use) so that users can organise their files into some hierarchical manner.!!)
- Character Device File : It refers to a physical device that transmits data in a character-based manner. (like printer, modems, consoles),
- Block Device File : Similar to character device file, but it can transmit a block of data at a time.
- FIFO FILE : A special pipe device file which furnishes a temporary buffer for 2 or more processes to communicate (by writing to / reading data from buffer).
Note: A single device can have both block and character device files representing them for different access methods.Consider F be a character device file on its creation. But F can be used by a hard disk to bulk/ non-blocking data transfer between a process and the disk.
Here is the complete program. . .
TYPECHECK.C
/*
PROGRAM NAME: TYPECHECK.C
AIM : TO READ A FILE NAME AND DISPLAY IT'S TYPE
DATE : 07/31/2011
*/
PROGRAM NAME: TYPECHECK.C
AIM : TO READ A FILE NAME AND DISPLAY IT'S TYPE
DATE : 07/31/2011
*/
#include<stdio.h>
#include<sys/stat.h>
main ()
{
char file_name[20];
struct stat p;
printf("\nEnter filename : ");
scanf("%s",file_name);
stat(file_name,&p);
printf("\a\n%s is a ",file_name);
if(S_ISREG(p.st_mode))
printf("regular file");
else if(S_ISDIR(p.st_mode))
printf("directory file");
#include<sys/stat.h>
main ()
{
char file_name[20];
struct stat p;
printf("\nEnter filename : ");
scanf("%s",file_name);
stat(file_name,&p);
printf("\a\n%s is a ",file_name);
if(S_ISREG(p.st_mode))
printf("regular file");
else if(S_ISDIR(p.st_mode))
printf("directory file");
else if(S_ISCHR(p.st_mode))
printf("character file");
else if(S_ISBLK(p.st_mode))
printf("block file");
else if(S_ISFIFO(p.st_mode))
printf("FIFO file");
else
printf("Unknown type file or not found..!!");
getch();
printf("character file");
else if(S_ISBLK(p.st_mode))
printf("block file");
else if(S_ISFIFO(p.st_mode))
printf("FIFO file");
else
printf("Unknown type file or not found..!!");
getch();
}
0 comments :
Post a Comment