In: Computer Science
C Programming:
Write a program that accepts 2 arguments, an input
file and an output file. The program is to store in the output file the contents of the input file in reverse. |
||
If the input file looks like this: |
|
|
then the output file should look like this: |
|
|
The main program should look like this:
int main(int argc, char **argv) { } argc is the number of arguments that are passed to the program + 1. argv[0] is the name of the program argv[1] is the FIRST argument passed to the program argv[2] is the SECOND argument passed to the program So if your program is named "assignment7" and you invoke the program as it is shown below:assignment7 inputFile.txt outputFile.txtthen argc is 3 (it specifies the length of the array argv which goes from 0 up to 3 (not including 3) argv[0] is the name of the program, in this case "assignment7" argv[1] is inputFile.txt argv[2] is outputFile.txt Here is a program that will get you started: fileRev.c. *****The only line that should be changed in fileRev.c is the line: /* INSERT YOUR CODE HERE */ Keep the rest of the code uneditted ******* The contenet of fileRev.c is: #include <fcntl.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main ( int argc, char *argv[]) { int i; int rr; int infile, outfile, filesize; char buffer[1]; if( argc !=3 ) { fprintf(stderr, "USAGE: %s inputFile outputFile.\n", argv[0]); exit(-1); } if( (infile = open(argv[1], O_RDONLY)) == -1) return(-1); if((outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644))==-1) { close(infile); return (-2); } filesize=lseek(infile, (off_t)0, SEEK_END); for(i=filesize-1; i>=0; i--) { /* * use lseek() to move the file pointer to the ith position * To set the file pointer to a position use the SEEK_SET flag * in lseek(). */ /* INSERT YOUR CODE HERE */ rr = read(infile, buffer, 1); /* read one byte */ if( rr != 1 ) { fprintf(stderr, "Couldn't read 1 byte [%d]\n", rr); exit(-1); } rr = write(outfile, buffer, 1); /* write the byte to the file*/ if( rr != 1 ) { fprintf(stderr, "Couldn't write 1 byte [%d]\n", rr); exit(-1); } } close(infile); close(outfile); return(0); } To compile type: gcc -o executable_name fileRev.c You are supposed to read one character at a time only,
no more than one. Implement this assignment using SEEK_END and SEEK_CUR as well as SEEK_SET. SEEK_END solution should look like: lseek(infile, (off_t)-(1 through 5), SEEK_END); SEEK_SET solution is lseek(infile, (off_t) i, SEEK_SET); SEEK_CUR solution should look like lseek(infile, (off_t)(only first time though should be -1 otherwise should be -2), SEEK_END); Use the lseek, open, close, read and write system calls. |
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main ( int argc, char *argv[]) {
int i;
int rr;
int infile, outfile, filesize;
char buffer;
if( argc !=3 ) {
fprintf(stderr, "USAGE: %s inputFile outputFile.\n", argv[0]);
exit(-1);
}
if( (infile = open(argv[1], O_RDONLY, 0)) == -1)
return(-1);
if((outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644))==-1) {
close(infile);
return (-2);
}
filesize=lseek(infile, (off_t)0, SEEK_END);
for(i=filesize-1; i>=0; i--) {
//Here i have added code to handle setting the
//file pointer in the ith position
lseek(infile, (off_t)i, SEEK_SET);
rr = read(infile, &buffer, 1); /* read one byte */
if( rr != 1 ) {
fprintf(stderr, "Couldn't read 1 byte [%d]\n", rr);
exit(-1);
}
rr = write(outfile, &buffer, 1); /* write the byte to the file*/
if( rr != 1 ) {
fprintf(stderr, "Couldn't write 1 byte [%d]\n", rr);
exit(-1);
}
}
close(infile);
close(outfile);
return(0);
}
Code Image:-
I have added the code to set the pointer in ith position using lseek SEEK_SET and reading the file.