Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I'm doing a C programming assignment on Ubuntu server. The goal is to write a program that copies the contents of a file exactly.

Hi I'm doing a C programming assignment on Ubuntu server. The goal is to write a program that copies the contents of a file exactly.

image text in transcribedThis is what I have. I am getting a segmentation fault when I try to run. Any advice?

#include #include #include #include #include

int error(char* msg) { perror(msg); return 2; }

int usage(char* name) { printf("Usage: %s ", name); return 1; } int main(int argc, char* argv[]) { if (argc != 3) return usage(argv[0]); // open files // read bytes from original file and // write to copy FILE *fr = fopen(argv[1], "r"); if(!fr) { error("file to copy does not exists"); } else { FILE *temp = fopen(argv[2], "r"); if(temp) { error("name of copy already exists"); } else { fclose(temp); FILE *fw = fopen(argv[2], "w"); if(fw) { char ch = 0; while((ch = getc(fr)) != EOF) { putc(ch, fw); } fclose(fw); } else { error("can not write to name of copy"); } } fclose(fr); } // close files return 0; }

1. The usage of mycopy is as follows: mycopy 2. If the file to be copied does not exist or the user lacks permission to read it, mycopy emits an error message and quits 3. If a file or directory already exists with the name proposed for the copy, mycopy emits an error and terminates. 4. You are to write code within the main function of the file mycopy.c, which is supplied with this assignment. thoupid with tnis asgsment. You may also add your own functions, if you wish. 1. The usage of mycopy is as follows: mycopy 2. If the file to be copied does not exist or the user lacks permission to read it, mycopy emits an error message and quits 3. If a file or directory already exists with the name proposed for the copy, mycopy emits an error and terminates. 4. You are to write code within the main function of the file mycopy.c, which is supplied with this assignment. thoupid with tnis asgsment. You may also add your own functions, if you wish

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MFDBS 89 2nd Symposium On Mathematical Fundamentals Of Database Systems Visegrad Hungary June 26 30 1989 Proceedings

Authors: Janos Demetrovics ,Bernhard Thalheim

1989th Edition

3540512519, 978-3540512516

More Books

Students also viewed these Databases questions

Question

pressure (atm)

Answered: 1 week ago

Question

8. Explain the difference between translation and interpretation.

Answered: 1 week ago

Question

10. Discuss the complexities of language policies.

Answered: 1 week ago

Question

1. Understand how verbal and nonverbal communication differ.

Answered: 1 week ago