Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that takes two command-line arguments that are file names, rea

ID: 3685624 • Letter: W

Question

Write a C program that takes two command-line arguments that are file names, reads the characters from the first file one at a time and writes the characters in reverse order to the second file. You can assume that the first file has at least one character and at most 1024 characters.

What I have so far:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[]){
FILE * finp;
FILE * fout;
int c;
char * charinp;
if(argc != 3){
printf(" Usage: a.out inputfile outputfile ");
exit(1);
}
if( (finp = fopen(argv[1], "r")) == NULL){
printf("Could not open %s for reading. ,",argv[1]);
exit(1);
}
if( (fout = fopen(argv[2], "w")) == NULL){
printf("Could not open %s for writing. ,",argv[2]);
exit(1);
}

charinp = argv[3];
while( (c = getc(finp)) != EOF){
// idk how to continue
}
return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[]){
FILE * finp;
FILE * fout;
char c;
int i,size;
char file1[1024];

if(argc != 3){
printf(" Usage: a.out inputfile outputfile ");
exit(1);
}
if( (finp = fopen(argv[1], "r")) == NULL){
printf("Could not open %s for reading. ,",argv[1]);
exit(1);
}
if( (fout = fopen(argv[2], "w")) == NULL){
printf("Could not open %s for writing. ,",argv[2]);
exit(1);
}

i=0;
while( (c = getc(finp)) != EOF){
   //read the first file entirely
   file1[i] = c;
   i++;
}
size = i; //size is the length of input file

//start from end and copy file1[] into your output file
for(i=size-1;i>=0;i--)
   putc(file1[i], fout);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote