Question

In: Computer Science

C Programming: Write a program that accepts 2 arguments, an input file and an output file....

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:
Hello World.\n
This is Line 2\n
This is the end.\n
 
then the output file should look like this:
\n
.dne eht si sihT\n
2 eniL si sihT\n
.dlroW olleH
 
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.txt
then 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.
DO NOT read the file in memory and then simply print it in reverse!

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.

Solutions

Expert Solution

#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.


Related Solutions

In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT