Question

Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE...

Consider the following, partially-complete, C-program:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

#define BUFFERSIZE 100
#define COPYMODE 0644

main(int argC, char* argv[])
{
   int srcFd;
   int dstFd;
   int charCnt;
   char buf[BUFFERSIZE];
   /*Check args*/
   if( argC!=3 ){
       fprintf( stderr, "usage: %s source destination\n", argv[0]);
       exit(1);
   }
   /*Open the files*/
  
   srcFd= open(argv[1],O_RDONLY);
   if( srcFd==-1 ){
       fprintf(stderr,"Cannot open %s \n", argv[1]);
   }

   dstFd= creat(argv[2],COPYMODE);

   if( dstFd==-1 ){
       fprintf( stderr,"Cannot create %s \n", argv[2]);
   }
   /* put your code to copy from srcFd to dstFd here */
   /* code to close files would go here. */
}
This program opens up a source file to be accessed with: srcFd and a
destination file to be accessed with: dstFd. There is code missing to
copy the contents of srcFd to dstFd.
Write C to to replace the comment:
/* put your code to copy from srcFd to dstFd here */
which copies the content of the source file to the destination file. Your
code should check for reading and writing errors as it goes and print
the appropriate messages to stderr. Note, you do not need to write
code to close the files.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Below is the code of the above problem.

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include<unistd.h>
#define BUFFERSIZE 100
#define COPYMODE 0644

int main(int argC, char* argv[])
{
   int srcFd;    //source file descriptor
   int dstFd;    //destination file descriptor
   int charCnt;   //variable to count no of character of transfer
   char buf[BUFFERSIZE]; //buffer to temporary store transfering data
   /*Check args*/
   if( argC!=3 ){
       fprintf( stderr, "usage: %s source destination\n", argv[0]);
       exit(1);
   }
   /*Open the files*/

   srcFd= open(argv[1],O_RDONLY);
   if( srcFd==-1 ){
       fprintf(stderr,"Cannot open %s \n", argv[1]);
   }

   dstFd= creat(argv[2],COPYMODE);

   if( dstFd==-1 ){
       fprintf( stderr,"Cannot create %s \n", argv[2]);
   }

/*code to copy from source file to destination file*/
//read system call take three arguments i.e source file descriptor,buffer, max size of data to read
//read return size of read data
//write is also same as read except that instead of source file descriptor, destination file descriptor is used.
   while((charCnt=read(srcFd,buf,BUFFERSIZE)) > 0)
       {
  
           if(write(dstFd,buf,charCnt) != charCnt)
               {
               perror("unable to write\n");
               exit(1);                  
           }
       }
/*code to close files*/
//close system call is used to close the file descriptor
//close takes file descriptor to be closed as argument
//on success close, return +ve integer
   if(close(srcFd)<0)
       {
           perror("unable to close source file descriptor\n");
           exit(1);
       }
   if(close(dstFd)<0)
       {
           perror("unable to close source file descriptor\n");
           exit(1);
       }

}

OUTPUT:

karan@karan-Inspiron-15-3567:~$ ./a.out file1.txt file2.txt

karan@karan-Inspiron-15-3567:~$ cat file1.txt

this is the text file to check
whether program is working fine or not
thank you

karan@karan-Inspiron-15-3567:~$ cat file2.txt
this is the text file to check
whether program is working fine or not
thank you

Add a comment
Know the answer?
Add Answer to:
Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...

    GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) {    // Variable declarations    int fd;                       // file descripter    char buffer[BUF_SIZE];       // string for...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

    Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.” Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL". Append that message to a file “7Error_Log_File.txt” . ?newline Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT