Wednesday, January 19, 2011

FILE HANDLING

// for simulating dos sopy command,showing file properties and for reversing a file
#include"stdio.h"
#include"unistd.h"
#include"sys/types.h"
#include"fcntl.h"
#include"time.h"
#include"sys/stat.h"
void main()
{ int fd,cho=0;char ch='y';char filename[10];char buffer[10];char c;
int fd2;int offset=-1;
struct stat nfile;
do{
printf("\n press 1 for dos copy command");
printf("\n press 2 for file attributes");
printf("\n press 3 for reverse of file");
scanf("%d",&cho);
printf("\n enter the file name *.*");
scanf("%s",filename);
fd=open(filename,O_RDONLY);
if(fd==-1)
{ printf("\n file cannot be opened \n");
cho=-1;
}
switch(cho)
{ case 1:fd2=creat("copy.c",0);
while(read(fd,&c,sizeof(char)))
{ write(fd2,&c,sizeof(char));
}
printf("\n copy file named copy.c created");

break;
case 2:stat(fd,&nfile);
printf("the user id is %d",nfile.st_uid);
printf("the size is %d",nfile.st_size);
printf("the creation time is %s",ctime(&nfile.st_ctime));
printf("the access time is %s",ctime(&nfile.st_atime));
printf("the modification time is %s",ctime(&nfile.st_mtime));
break;
case 3: fd2=creat("rev.c",0);
while((lseek(fd,offset,SEEK_END)!=-1))
{ read(fd,&ch,sizeof(ch));
write(fd2,&ch,sizeof(ch));
offset--;
}
printf("the reversed file rev.c created!");

break;
default:if(cho!=-1)printf("wrong choice");
}
printf("\n do you want to continue?y/n?");
scanf("%c",&ch);
}while(ch=='y');

}

No comments:

Post a Comment