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

So I have this homework question: In Section 2.3, we described a program that co

ID: 3587324 • Letter: S

Question

So I have this homework question:

In Section 2.3, we described a program that copies the contents of one file to a destination file. This program works by first prompting the user for the name of the source and destination files. Write this program using either the Win32 or POSIX API. Be sure to include all necessary error checking, including ensuring that the source file exists. Once you have correctly designed and tested the program, if you used a system that supports it, run the program using a utility that traces system calls. Linux systems provide the ptrace utility, and Solaris systems use the truss or dtrace command. On Mac OS X, the ktrace facility provides similar functionality. As Windows systems do not provide such features, you will have to trace through the Win32 version of this program using a debugger.

I already wrote a c program that reads and write the file but I don't know what POSIX API is or how to us ptrace. I'm using ubuntu.

// CS 474
// Program Name: homework1.c
// Author: Melissa Duran
// Date: 10/5/2017
// Purpose: Reads and copies what is written in one file to another

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *cfPtr1;
FILE *cfPtr2;
char whatIWrite;

int main (void) {
char *fileToRead = malloc(101);
char *fileToWrite = malloc(101);
  
printf("Please enter the name of the file to be read: ");
scanf("%s",fileToRead);
  
//if file is read as empty retruns teh text File could not be opened
if ( (cfPtr1 = fopen(fileToRead, "r" )) == NULL) {
puts( "File could not be opened ");
}
  
printf("Please enter the name of the file where you would like to write: ");
scanf("%s", fileToWrite);
//if file is read as empty retruns teh text File could not be opened
if ( (cfPtr2 = fopen(fileToWrite, "w" )) == NULL) {
puts( "File could not be opened ");
}
  
//Everything is well and we can copy the files.
else{
  
//Read from fileToRead
whatIWrite = fgetc(cfPtr1);
while (whatIWrite != EOF){
fputc(whatIWrite, cfPtr2);
whatIWrite = fgetc(cfPtr1);
}

printf("The file has been copied.");
  
//closes the file
fclose ( cfPtr1 );
fclose ( cfPtr2 );
}//End else
return 0;
  
} // end main

Explanation / Answer

CODE

#include <stdio.h>
#include <stdlib.h>  
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading ");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}

printf("Enter the filename to open for writing ");
scanf("%s", filename);

fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf(" Contents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

IF ANY QUERIES REGARDING CODE PLEASE GET BACK TO ME
THANK YOU

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