Question

In: Computer Science

Take the following code and modify the if(args >= 3) statement to work with a dynamic...

Take the following code and modify the if(args >= 3) statement to work with a dynamic amount of inputs.

Example: ./cat file1 file2 file3 file4 filen-1 filen should output a text file that concatenates the files file1 to filen into one file

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   char ch;

   if (argc ==1)
   {
       while((ch = fgetc(stdin)) != EOF) fputc(ch, stdout);
   }

   if (argc == 2)
   {
       File *fp = fopen(argv[1], "r");

       if (fp == NULL)
       {
           printf("File doesn't exist\n");
           return -1;
       {

       while ((ch = fgetc(fp)) != EOF) putchar(ch);
   }

   //ADD ARRAY OF ARGS
   if (argc >= 3)
   {
       // Open files to be merged

     // Open file to store the result

//change to accept filen+1 files if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
       {
        puts("Could not open files");
       exit(0);
       }

//change to copy into filen+1

       // Copy contents of first file to filen+1.txt
       while ((ch = fgetc(fp1)) != EOF) fputc(ch, stdout);

       // Copy contents of second file to file3.txt
       while ((ch = fgetc(fp2)) != EOF) fputc(ch, stdout);

       fclose(fp1);
       fclose(fp2);
   {

   retrun 0;  
}

Solutions

Expert Solution

That should be FILE, not File

Here is code:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char **argv)

{

char ch;

if (argc == 1)

{

printf("Invalid inputs\n");

}

if (argc == 2)
   {
       File *fp = fopen(argv[1], "r");

       if (fp == NULL)
       {
           printf("File doesn't exist\n");
           return -1;
       {

       while ((ch = fgetc(fp)) != EOF) putchar(ch);
   }

   //ADD ARRAY OF ARGS
   if (argc >= 3)
   {

FILE *f2 = fopen(argv[argc - 1], "a+");

for (int i = 1; i < argc - 1; i++)

{

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

FILE *fp = fopen(argv[i], "r");

if (fp == NULL)

{

printf("%s File doesn't exist\n", argv[i]);

}

else

{

while ((ch = fgetc(fp)) != EOF)

{

fprintf(f2, "%c", ch);

}

fprintf(f2, "\n");

}

fclose(fp);

}

fclose(f2);

}

return 0;

}

file1.txt:

at velit vivamus vel nulla eget eros elementum pellentesque quisque porta volutpat erat quisque erat eros viverra eget
dictumst maecenas ut massa quis augue luctus tincidunt nulla mollis molestie lorem quisque
nunc nisl duis bibendum felis sed interdum venenatis turpis enim blandit
interdum mauris non ligula pellentesque ultrices phasellus id sapien in sapien iaculis congue vivamus metus arcu adipiscing
pulvinar nulla pede ullamcorper augue a suscipit nulla elit ac nulla sed vel enim sit

file2.txt:

sapien sociis natoque penatibus et magnis dis parturient montes nascetur ridiculus mus etiam vel
arcu sed augue aliquam erat volutpat in congue etiam justo etiam pretium iaculis justo in hac habitasse
eros viverra eget congue eget semper rutrum nulla nunc purus phasellus in felis donec semper sapien a libero
nibh in quis justo maecenas rhoncus aliquam lacus morbi quis tortor id nulla ultrices aliquet
dui vel nisl duis ac nibh fusce lacus purus aliquet at

Output:

file3.txt:


Related Solutions

Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
Modify the linked list code from class to work with strings. Insert the following food items...
Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list. c++
take the following html code and make it work for html validator. heres the ,html file...
take the following html code and make it work for html validator. heres the ,html file <!DOCTYPE html> <html lang="en">    <head>        <title> GettingStarted</title>        <meta charset="utf-8">        <link href="Style.css" rel="stylesheet">    </head>       <body>        <header><h1>GettingStarted</h1></header>        <nav>               <b>        <a href="Home.html">Home</a>&nbsp;        <a href="GettingStarted.html">Getting Started</a>&nbsp;        <a href="MaterialsNeeded.html">Materials Needed</a>&nbsp;                      <a href="TroubleShooting.html">TroubleShooting</a>&nbsp;        <a href="InfoMaterials.html">Infomation on materials</a>&nbsp;   ...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘encryptECB(key, secret_message)’ that accepts key and secret_message and returns a ciphertext.          #Electronic Code Block AES algorithm, Encryption Implementation from base64 import b64encode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes secret_message = b" Please send me the fake passport..." password = input ("Enter password to encrypt your message: ") key= pad(password.encode(), 16) cipher...
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify...
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘decryptECB(key, ciphertext)’ that accepts key and ciphertext and returns a plaintext. from base64 import b64decode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.Padding import unpad # We assume that the password was securely shared beforehand password = input ("Enter the password to decrypt the message: ") key= pad(password.encode(), 16) ciphertext = input ("Paste the cipher...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set by PASSING IN the arguments for the X and Y coordinates. The Z coordinate can be hard-coded (extra credit if it is passed in too). ADD CODE to print the new point location, and note to the user it is the new point using input arguments Add 2 new methods to the Point3d class, each accepting 3 values as input. pAdd(a1, a2, a3) will...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
Modify the following java code, utilizing a loop mechanism to enable the user to use the...
Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once. The program does the following:    It prompts the user to enter 2 numbers.    It prompts the user to choose an operation to perform on those numbers:    Operation 1: Addition.    Operation 2: Subtraction.    Operation 3: Multiplication.    Operation 4: Division.    It outputs the result of the operation.    It asks the user if they want...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item on 1st column, price on 2nd) Stewing beef,15.45 Ground beef,11.29 Pork chops,11.72 Chicken,7.29 Bacon,7.12 Wieners,4.33 Canned salmon,5.68 Homogenized milk,5.79 Partly skimmed milk,5.20 Butter,4.99 Processed cheese slices,2.53 Evaporated milk,1.89 Eggs,3.11 Bread,2.74 Soda crackers,3.27 Macaroni,1.45 Flour,4.54 Corn flakes,5.72 Apples,4.71 Bananas,1.56 Oranges,3.70 ... a. In the function createPricesDict(), create a dictionary of each product mapped to its price. b. Suppose we have another dictionary for our cart...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT