Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character becomes the last character of the file and so on) Input: The program takes the file name whose content should be reversed. Output: The program reverses the contents of the file.

Questions by souji1425

Showing Answers 1 - 8 of 8 Answers

bmlal

  • Jul 14th, 2006
 

As we know, there are so many logic we can use to slove the problem.

I will give one of the solution to this problem.

Usage:

$./a.out <filename>

Example:

$./a.out data

Now check the contents of the data file.

  Was this answer useful?  Yes

bmlal

  • Jul 14th, 2006
 

#include #include int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); if (argc != 2) { printf("pass two argumentsn"); exit (-1); } /* Open the file */ fd = open(argv[1], O_RDWR); if (fd == -1) { printf("error in file openingn"); exit (-1); }#ifdef PRINT printf("File Open Sucessn");#endif /* Find the file size */ fsize = lseek(fd, 0, SEEK_END);#ifdef PRINT printf("File Size = %dn", fsize);#endif /* Reverse the file */ for (i=0; i

  Was this answer useful?  Yes

bmlal

  • Jul 14th, 2006
 

#include #include int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); fd = open(argv[1], O_RDWR); fsize = lseek(fd, 0, SEEK_END); for (i=0; i

  Was this answer useful?  Yes

bmlal

  • Jul 14th, 2006
 

I am not sure why my code is not getting pasted. I will try it one more time. int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); fd = open(argv[1], O_RDWR); fsize = lseek(fd, 0, SEEK_END); for (i=0; i

  Was this answer useful?  Yes

bmlal

  • Jul 14th, 2006
 

int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); fd = open(argv[1], O_RDWR); fsize = lseek(fd, 0, SEEK_END); for (i=0; i lessthan fsize/2; i++) { lseek(fd, i, SEEK_SET); read(fd, first, 1); lseek(fd, -i-2, SEEK_END); read(fd, last, 1); lseek(fd, i, SEEK_SET); write(fd, last, 1); lseek(fd, -i-2, SEEK_END); write(fd, first, 1); } }

  Was this answer useful?  Yes

y.swathi

  • Jan 29th, 2007
 

its very useful

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions