Question

Write a C program which is called ‘multiple_copy’. This program copies one source file to two...

Write a C program which is called ‘multiple_copy’. This program copies one source file to two destination files as follows:

$./multiple_copy....... source_file....... destination_file1......... destination_file2

  1. multiple_copy program copies the contents of source file (i.e., source_file) to any named two destination files in the command line.

  2. The number of arguments should be 4 including multiple_copy. If the number of arguments is not 4, the program should display error message saying:

    “Usage: multiple_copy source_file destination_file1 destination_file2”.

  3. When the source_file does not exist, the program should display error

    message with source file name, for example, “Opening Error!: source_file”.

  4. If the destination file exists, existing content of that file should be truncated

    (removed).

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

Hi, Please find the program.

//

// main.c

// CopySourceTo 2 dest Files

//

// Created by Abhirama Gopala Dasa on 16/09/19.

// Copyright © 2019 Abhirama Gopala Dasa. All rights reserved.

//

#include <stdio.h>

int main(int argc, const char * argv[]) {

for (int i=0; i<argc; i++) {

printf("%s\n",argv[i]);

}

  

if(argc!=4){

printf("Please input sourceFile,and 2 destination files\n");

return 0;

}

FILE *source;

FILE *dest1,*dest2;

char data[100];

printf("%s",argv[1]);

source = fopen(argv[1], "r");

dest1 = fopen(argv[2], "w");

dest2 = fopen(argv[3], "w");

if(source==NULL){

printf("Failed to open file");

return 0;

}

while(fgets(data,100,source)!=NULL){

printf("%s",data);

  

fputs(data, dest1);

// fputs("\n", dest1);

  

fputs(data,dest2);

// fputs("\n", dest2);

}

  

fclose(dest1);

fclose(dest2);

printf("\nCopyDone\n");

}

Please make sure the file paths do not contain spaces. thanks. copying is working perfect.

Add a comment
Know the answer?
Add Answer to:
Write a C program which is called ‘multiple_copy’. This program copies one source file to two...
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
  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write the pseudocode for an algorithm called copystack that copies the contents of one stack into...

    Write the pseudocode for an algorithm called copystack that copies the contents of one stack into another. The algorithm passes two stacks, the source stack and the destination stack. The order of the stacks must be identical. Note: please number the steps, Thanks

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
ADVERTISEMENT