Question

Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

Convert C to C++

I need these 4 C file code convert to C++. Please Convert it to C++

//////first C file: Wunzip.c

#include

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

if(argc ==1){

printf("wunzip: file1 [file2 ...]\n");

return 1;

}

else{

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

int num=-1;

int numout=-1;

int c;

int c1;

  

FILE* file = fopen(argv[i],"rb");

if(file == NULL){

printf("Cannot Open File\n");

return 1;

}

else{

while(numout != 0){

  

numout = fread(&num, sizeof(int), 1, file);

  

c = fread(&c1, sizeof(char), 1, file);

if(c!= 0){

for(int a =0;a< num;a++){

printf("%c",c1);

}}

}

}

fclose(file);

}

return 0;

}

  

}

/// Second C file: wgrep.c

#include

#include

#include

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

if(argc ==1){

printf("wgrep: searchterm [file ...]\n");

return 1;

}

else if(argc ==2){

size_t len = 0;

ssize_t linesize=0;

char* output = NULL;

int counter =1;

  

  

while((linesize = getline(&output, &len, stdin)) != -1){

  

   if(strstr(output, argv[1])){

printf( "%s",output);

   counter++;

   }

  

}

return 0;

}

else{

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

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

if (file == NULL){

printf("wgrep: cannot open file\n");

return 1;

}

size_t len = 0;

ssize_t linesize=0;

char* output = NULL;

int counter =1;

while((linesize = getline(&output, &len, file)) != -1){

   if(strstr(output, argv[1])){

   printf( "%s",output);

   }

counter++;

  

}

fclose(file);

free(output);

output = NULL;

len = 0;

   }

}

  

return 0;

}

///////Third C file: wcat.c

#include

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

if(argc == 1){

return 0;

}

  

//implementing Wcat

for(int i =1;i

char totalBuf[100];

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

if (myFile == NULL) {

printf("wcat: cannot open file\n");

return 1;

}

while(fgets(totalBuf, 100, myFile) != NULL){

  

printf("%s",totalBuf);

}

fclose(myFile);

}

return 0;

}

/////////Four C file: wzip.c

#include

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

char currentC;

char previousC;

int count = 1;

int stop =1;

if(argc == 1 ){

printf( "wzip: file1 [file2 ...]\n");

return 1;

}

else{

for(int i =1;i

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

if(file == NULL){

   printf("wzip: cannot open file\n");

   return 1;

}

else{

   previousC = fgetc(file);

// FILE *file1 = fopen("newfile","w") ;

while((currentC = fgetc(file)) != EOF){

if(previousC == currentC){

count++;

}

else{

fwrite(&count, 4, 1, stdout);

fwrite(&previousC, sizeof(char), 1, stdout);

previousC = currentC;

count =1;

}

}

fwrite(&count, 4, 1, stdout);

fwrite(&previousC, sizeof(char), 1, stdout);

}

fclose(file);

}

return 0;

}

}

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

The that you have made is a basic C program that is almost the same like C++ for proper conversion you have to change the header file and some standard output command printf to cout. control follow statement and loops are written in same as C in c++.

//////first C file: Wunzip.c

#include<bits/stdc++.h>//Header file for almost all standard library in cpp
using namespace std;//all the standard function like count cin etc are availabe in this group
int main(int argc, char* argv[])
{
   if(argc == 1){
   cout<<"wunzip: file1 [file2 ...]\n";
   return 1;
   }
   else{
       for(int i =1; i< argc;i++){
           int num=-1;
           int numout=-1;
           int c;
           int c1;
           FILE* file = fopen(argv[i],"rb");
           if(file == NULL){
               //cout instead of printf is used in c++
               cout<<"Cannot Open File\n";
               return 1;
           }
           else{
               while(numout != 0){
                   numout = fread(&num, sizeof(int), 1, file);
                   c = fread(&c1, sizeof(char), 1, file);
                   if(c!= 0){
                       for(int a =0;a< num;a++){
                       /*cout instead of printf is used in c++ and it
                       doesn't use identifier like "%d" that we use in printf*/
                       cout<<c1;
                       }
                   }
               }
           }
           fclose(file);
       }
return 0;
}
}

/// Second C file: wgrep.c

#include<bits/stdc++.h>//Header file for almost all standard library in cpp
using namespace std;//all the standard function like count cin etc are availabe in this group
int main(int argc, char* argv[]) {
if(argc == 1){
cout<<"wgrep: searchterm [file ...]\n";
return 1;
}
else if(argc ==2){
size_t len = 0;
ssize_t linesize=0;
char* output = NULL;
int counter =1;
while((linesize = getline(&output, &len, stdin)) != -1){
   if(strstr(output, argv[1])){
   /*cout instead of printf is used in c++ and it
   doesn't use identifier like "%d" that we use in printf*/
   cout<<output;
   counter++;
   }
}
return 0;
}
else{
   for(int i =2;i< argc;i++){
FILE *file = fopen(argv[i], "r");
if (file == NULL){
//cout instead of printf is used in c++
cout<<"wgrep: cannot open file\n";
return 1;
}
size_t len = 0;
ssize_t linesize=0;
char* output = NULL;
int counter =1;
while((linesize = getline(&output, &len, file)) != -1){
   if(strstr(output, argv[1])){
   /*cout instead of printf is used in c++ and it
   doesn't use identifier like "%d" that we use in printf*/
   cout<<output;
   }
counter++;
}
fclose(file);
free(output);
output = NULL;
len = 0;
   }
}
return 0;
}

///////Third C file: wcat.c

#include<bits/stdc++.h>//Header file for almost all standard library in cpp
using namespace std;//all the standard function like count cin etc are availabe in this group
int main(int argc, char* argv[]) {
   if(argc == 1){
   return 0;
   }
//implementing Wcat
   for(int i =1;i< argc;i++){
       char totalBuf[100];
       FILE *myFile = fopen(argv[i], "r");
       if (myFile == NULL) {
           cout<<"wcat: cannot open file\n";
           return 1;
       }
       while(fgets(totalBuf, 100, myFile) != NULL){
           cout<<totalBuf;
           }
       fclose(myFile);

       }

return 0;

}

/////////Four C file: wzip.c

#include<bits/stdc++.h>//Header file for almost all standard library in cpp
using namespace std;//all the standard function like count cin etc are availabe in this group
int main(int argc, char* argv[]) {
char currentC;
char previousC;
int count = 1;
int stop =1;
if(argc == 1 ){
   cout<<"wzip: file1 [file2 ...]\n";
   return 1;}
else{for(int i =1;i< argc;i++){
       FILE *file = fopen(argv[i],"r") ;
       if(file == NULL){
        cout<<"wzip: cannot open file\n";
       return 1;
       }
       else{
        previousC = fgetc(file);
       // FILE *file1 = fopen("newfile","w") ;
       while((currentC = fgetc(file)) != EOF){
           if(previousC == currentC){
               count++;
           }else{
               fwrite(&count, 4, 1, stdout);
               fwrite(&previousC, sizeof(char), 1, stdout);
               previousC = currentC;
               count =1;  
               }}
           fwrite(&count, 4, 1, stdout);
           fwrite(&previousC, sizeof(char), 1, stdout);
           }

       fclose(file);   }   return 0;
   }
}

Screenshot of code

Note:- Because I don't have the input file, I can't show you result or output

Add a comment
Know the answer?
Add Answer to:
Convert C to C++ I need these 4 C file code convert to C++. Please Convert...
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
  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Below is a basic implementation of the Linux command "cat". This command is used to print...

    Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

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