1. Introduction Use command line arguments and files in C so that a new command
ID: 3599199 • Letter: 1
Question
1. Introduction
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
Explanation:
If the user does not mention anything then the entire file is copied
If the n is the input the the nth line is copied
If -m is the input then lines from the start till line m are copied ( including mth line)
If n- is the input then lines staring from nth line till the end of the file are copied.
If n-m is the input then lines staring from nth line til the mth line are copied.
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
Explanation / Answer
Here is the code for you:
#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.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.