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

Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
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++
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal =...
Troubleshoot the code to create a dynamic input field to take numerical inputs and label strings...
Troubleshoot the code to create a dynamic input field to take numerical inputs and label strings to draw a pie chart. <html> <head> <title>AddingRows</title> </head> <header>     <h1>AddingRows</h1> </header> <style> table, td {     border-style:double;     border-color: black; } </style> <body>     <table id="shtol">         <tr>           <td>Label 2 </td>             <td><input type="text" class = "labelIn"/></td>           <td>Percentage</td>           <td><input type="number" class = "percentIn"/></td>         </tr>         <tr>             <td>Label 1 </td>               <td><input type="text" class = "labelIn"/></td>             <td>Percentage</td>             <td><input type="number" class = "percentIn"/></td>         </tr>       </table>       <br>       <button type="button" onclick="addR()">Add Label</button>       <button type="button"...
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;   ...
The code must work on PEP/9, it shouldn't be too complicated and long Take the following...
The code must work on PEP/9, it shouldn't be too complicated and long Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT