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

c programing language question 1. Introduction The purpose of this assignment is

ID: 3601579 • Letter: C

Question

c programing language question

1. Introduction

The purpose of this assignment is to use command line arguments and files in C so that a new command may be designed. The command we will create is scopy i.e. selective copy that is a variation to copy only some number of lines if user wishes to enter a number

2. Instructions

You can use nano or any other editor to write your C code.

The UNIX command would be replicating the Windows COPY command with the only exception that you may define the number of lines to copy.

Command name: scopy (selective copy)

Arguments :

Required : source file name, target file name

Optional : number of lines as an integer or range, -> n, -m, n- or n-m

3. Steps:

1. Make sure both source file and target file names are given and are valid

2. Make sure that the source file exists whereas target file DOES NOT EXIST.

3. No file should be overwritten without confirmation

4. If the number of lines is not mentioned, the entire file is copied

Thanks

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sCopy(FILE* src, FILE* dest, int start, int end)
{
    char *line = (char *)malloc(256);
   size_t bufSize = 256;
   for(int i = 1; i < end && !feof(src); i++)   //Reads the lines till start.
       getline(&line, &bufSize, src);           //Do nothing with the lines.
   for(int i = start; i <= end || !feof(src); i++)
   {
      getline(&line, &bufSize, stdin);
      fprintf(dest, "%s ", line);   //Writes the line to output file.
   }  
}
int main(int argc, char* argv[])
{
    if(argc == 1 || argc > 4)
    {
       printf("ERROR: Invalid syntax. Syntax: command [options] source target ");
       return 0;
    }
    FILE* fin; FILE* fout;
    if(argc == 3)   //No options.
    {
       fin = fopen(argv[1], "r");
       fout = fopen(argv[2], "r");
       if(fout != NULL)
       {
          printf("Output file readily exist. Can't overwrite. ");
          return 0;
       }   
       fout = fopen(argv[2], "w");
       sCopy(fin, fout, 1, 1000);   //Assuming a file won't have more than 1000 lines.  
       return 0;
    }
    char option[3];
    int start, end;
    strcpy(option, argv[1]);
    fin = fopen(argv[2], "r");
    fout = fopen(argv[3], "r");
    if(fout != NULL)
    {
       printf("Output file readily exist. Can't overwrite. ");
       return 0;
    }  
    fout = fopen(argv[3], "w");
   
    if(strlen(option) == 1)
        start = end = option[0] - '0';
    else if(option[0] == '-')  
    {
       start = 1;
       end = option[1] - '0';
    }
    else if(option[1] == '-' && strlen(option) == 2)
    {
       start = option[0] - '0';
       end = 1000;
    }
    else
    {
       start = option[0] - '0';
       end = option[2] - '0';
    }
       sCopy(fin, fout, start, end);   //Assuming a file won't have more than 1000 lines.  
}

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