Question
these are 3 functions and I need these functions to C++ I need this all C code converting to C++. Please convert this code to
these are 3 functions and I need these functions to C++
I need this all C code converting to C++.
Please convert this code to C++
Function for CAT :
#include #include #include #include int main(int argc,char *argv[]) { // fd - file descriptor int fd, i, chr; for (i = 1; i < argc; i++) { // open file in read only mode fd = open(argv[1], O_RDONLY); // error handlings if(fd < 0) { perror("Unable to open file!"); return -1; } // reading file byte by byte while(read(fd, &chr, 1)) // writing byte to stdout write(STDOUT_FILENO, &chr, 1);
// closing fd close(fd); } return 0; }
FUNCTION FOR GREP
#include #include #include
int main(int argc,char *argv[]) {
// reading file FILE *fp = fopen(argv[2], "r");
// error handling if(fp == NULL) { perror("Unable to open file!"); return -1; }
// declaring chunk // will use it for every line char chunk[128];
// iterating over file line by line while(fgets(chunk, sizeof(chunk), fp) != NULL) { // checking if pattern exists in line if(strstr(chunk, argv[1])){ // setting the line to stdout fputs(chunk, stdout); } }
fclose(fp); return 0; }
FUNCTION FOR ZIP
#include #include #include #include #define MAX_RLEN 500
int getSize(char * s) { char * t; for (t = s;* t != '\0'; t++) ; return t - s; }
char * zip(char * str);
int main(int argc,char *argv[]) {
FILE *fp; char *buffer; long bytes;
fp = fopen ( argv[1] , "rb" );
if( fp == NULL ){ perror("Unable to open file!"); return -1; }
// get number of bytes in file fseek( fp , 0L , SEEK_END); bytes = ftell( fp ); // reset seek to start fseek( fp, 0L, SEEK_SET);
// allocate memory for entire content buffer = (char*)calloc(bytes, sizeof(char)); fread(buffer, sizeof(char), bytes, fp); fclose(fp);
// using zip function for RLE char * encoded = zip(buffer); fputs(encoded, stdout);
return 0;
}
char * zip(char * str) { int rLen; char count[MAX_RLEN]; int len = strlen(str);
// dynamic allocation for final encoding char * dest = (char * ) malloc(sizeof(char) * (len * 2 + 1));
int i, j = 0, k;
// iterating over the input string for (i = 0; i < len; i++) {
// copy first occurence of the new chahrater dest[j++] = str[i];
// counting occurence of caharacter rLen = 1; while (i + 1 < len && str[i] == str[i + 1]) { rLen++; i++; }
// storing repeat occurences in character array count sprintf(count, "%d", rLen);
// copy occurence count to destination for (k = 0;*(count + k); k++, j++) { dest[j] = count[k]; } }
// terminating the destination string dest[j] = '\0'; return dest; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started