EmbLogic's Blog

program to read text from a file and store it in reverse order

#include<stdio.h>
#include<fcntl.h>
int openf(char *);
int main(int argc, char * argv[])
{
int fd,fd1, pos,i=3;
char ch;
fd = openf(argv[1]);
fd1 = openf(argv[2]);
pos = lseek(fd, 0, SEEK_END);
printf(“\n%d\n”,pos);
for(i=2; i < pos+1;i++)
{
lseek(fd, pos-i, SEEK_SET);
read(fd, &ch, 1);
write(fd1, &ch, 1);
printf(“ch = %c\n”,ch);
}

close(fd);
close(fd1);
}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>