Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to convert this code to C #include<stdio.h> #include<fstream> #include<ct

ID: 3726715 • Letter: I

Question

I need to convert this code to C

#include<stdio.h>

#include<fstream>

#include<ctime>

#include<string>

#include<iostream>

//Use namespace

using namespace std;

//Define method

char* readableTime(long unsigned int sec)

{

    //Create instance

    time_t t = (time_t)sec;

    //Return

    return asctime(localtime(&t));

}

//Define method

void process(ifstream &file,long unsigned int time,int n)

{

    //Call method

    char *t = readableTime(time);

    //Declare variable

    string display(t);

    //If n is 0

    if(n == 0)

    {

        //Assign value

        display[display.length()-1] = ' ';

        //Assign value

        display += " From ";

    }

    //If n is 1

    if(n == 1)

    {

        //Assign value

        display[display.length()-1] = ' ';

        //Assign value

       display += "   To ";

    }

    //Declare variable

    int wor = 0;

    //Declare variable

    string phone;

    //Store

    file>>phone;

    //Loop

    for(int i = 0;i<phone.length();i++)

    {   

        //If condition satisfies

        if(i != 0 && i%3 == 0 && i<7)

        {

            //Assign value

            display += "-";

        }

        //Update value

        display += phone[i];

    }

    //Assign value

    display += " | ";

    //Store

    file>>wor;

    //Declare variable

    char ch;

    //Loop

    for(int i = 0;i<=wor;)

    {

        //Get

        file.get(ch);

        //If condition satisfies

        if(i != wor)

        {   

             //Assign value

            display += ch;

        }

        //If condition satisfies

        if(ch == ' ' || ch == ' ')

        {

            //If condition satisfies

            if(ch == ' ' && i != wor)

            {

                //Assign value

                display += "                                            | ";

            }

            //Increment

            i++;

        }

    }

    //Display

    //printf("%s",display);

    cout<<" "<<display;

}

//Define main

int main(int argc,char **argv)

{

    //Declare variable

    long unsigned int time1,time2;

    //If condition satisfies

    if(argc != 3)

    {

        //Display message

       printf(" Not Enough Argument !");

        //Return

        return -1;

    }

    //Call method

    string recieve(argv[0]);

    //Call method

    string sent(argv[1]);

    //Declare variable

    ifstream rfs;

    //Declare variable

    ifstream sfs;

    //Open

    rfs.open(recieve.c_str());

    //Open

    sfs.open(sent.c_str());

    //If condition satisfies

    if(rfs.is_open() && sfs.is_open())

    {

    }

    //Otherwise

    else

    {

        //Display message

        printf(" File Opening Error check files names ");

        //Return

        return -1;

    }

    //Store

    rfs>>time1;

    //Store

    sfs>>time2;

    //Loop

    while(!rfs.eof() && !sfs.eof())

    {

        //If condition satisfies

        if(time1 < time2)

        {

            //Call method

            process(rfs,time1,0);

            //If condition satisfies

            if(rfs.eof())

            {

                //Break

                break;

            }

            //Store

            rfs>>time1;

        }

        //Otherwise

        else

        {

            //Call method

            process(sfs,time2,1);

            //If condition satisfies

            if(sfs.eof())

            {

                //Break

                break;

            }

            //Store

            sfs>>time2;

        }

    }

    //Loop

    while(!rfs.eof())

    {

        //Call method

        process(rfs,time1,0);

        //If condition satisfies

        if(rfs.eof())

        {

            //Break

            break;

        }

        //Store value

        rfs>>time1;

    }

    //Loop

    while(!sfs.eof())

    {

        //Call method

        process(sfs,time2,1);

        //If condition satisfies

        if(sfs.eof())

        {

            //Break

            break;

        }

        //Store value

        sfs>>time2;

    }

    //Pause console window

    getchar();

    //Pause console window

    getchar();

  

    //Return

    return 0;

}

Explanation / Answer

/**************** Converted the Entire Code to C *************/


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

#include<time.h>

#include<string.h>

//using namespace std;

//Define method

char* readableTime(long unsigned int sec)

{

    //Create instance

    time_t t = (time_t)sec;

    //Return

    return asctime(localtime(&t));

}

//Define method

void process(FILE *file,long unsigned int time,int n)

{

    //Call method

    char *t = readableTime(time);
   
   
    //Declare variable

    char *display = t;

    //If n is 0

    if(n == 0)

    {

        //Assign value

        display[strlen(display)-1] = ' ';
       
        //Assign value
       
        char * dest = " From ";
        char *temp;
        temp = malloc(strlen(display) +strlen(dest) + 1);
        strcpy(temp, display);
        strcat(temp, dest);
        display = temp;
    }

    //If n is 1

    if(n == 1)

    {

        //Assign value

        display[strlen(display)-1] = ' ';

        //Assign value
        char * dest = "   To ";
        char *temp;
        temp = malloc(strlen(display) +strlen(dest) + 1);
        strcpy(temp, display);
        strcat(temp, dest);
        display = temp;
      

    }

    //Declare variable

    int wor = 0;

    //Declare variable

    char *phone;

    //Store
    fscanf(file,"%s",&phone);

    //Loop

    for(int i = 0;i<strlen(phone);i++)

    {  

        //If condition satisfies

        if(i != 0 && i%3 == 0 && i<7)

        {

            //Assign value
            char * dest = "-";
            char *temp;
            temp = malloc(strlen(display) +strlen(dest) + 1);
            strcpy(temp, display);
            strcat(temp, dest);
            display = temp;
            //display += "-";

        }

        //Update value

        display += phone[i];

    }

    //Assign value
    char * dest =" | ";
    char *temp;
    temp = malloc(strlen(display) +strlen(dest) + 1);
    strcpy(temp, display);
    strcat(temp, dest);
    display = temp;
    //display += " | ";

    //Store
    fscanf(file,"%d",&wor);
    //file>>wor;

    //Declare variable

    char ch;

    //Loop

    for(int i = 0;i<=wor;)

    {

        //Get

        ch = (char)fgetc(file);

        //If condition satisfies

        if(i != wor)

        {  

             //Assign value
            
            char dest [1];
            dest[0] = ch;
            char *temp;
            temp = (char *)malloc(strlen(display) +strlen(dest) + 1);
            strcpy(temp, display);
            strcat(temp, dest);
            display = dest;
           

        }

        //If condition satisfies

        if(ch == ' ' || ch == ' ')

        {

            //If condition satisfies

            if(ch == ' ' && i != wor)

            {

                //Assign value
                char * dest ="                                            | ";
                char *temp;
                temp = (char *)malloc(strlen(display) +strlen(dest) + 1);
                strcpy(temp, display);
                strcat(temp, dest);
                display = dest;


            }

            //Increment

            i++;

        }

    }

    //Display

    printf("%s",display);

   

}

//Define main

int main(int argc,char **argv)

{

    //Declare variable

    long unsigned int time1,time2;

    //If condition satisfies

    if(argc != 3)

    {

        //Display message

       printf(" Not Enough Argument !");

        //Return

        return -1;

    }

    //Call method

    char *recieve = argv[0];

    //Call method

    char *sent = argv[1];

    //Declare variable

    FILE *rfs;

    //Declare variable

    FILE *sfs;

    //Open

    rfs = fopen(recieve,"r+");

    //Open

    sfs = fopen(sent,"r+");

    //If condition satisfies

    if(rfs !=NULL && sfs !=NULL)

    {

    }

    //Otherwise

    else

    {

        //Display message

        printf(" File Opening Error check files names ");

        //Return

        return -1;

    }

    //Store
    fscanf(rfs,"%d",&time1);
    //rfs>>time1;

    //Store
    fscanf(sfs,"%d",&time2);
    //sfs>>time2;

    //Loop

    while(!feof(rfs) && !feof(sfs))

    {

        //If condition satisfies

        if(time1 < time2)

        {

            //Call method

            process(rfs,time1,0);

            //If condition satisfies

            if(feof(rfs))

            {

                //Break

                break;

            }

            //Store

            fscanf(rfs,"%d",&time1);

        }

        //Otherwise

        else

        {

            //Call method

            process(sfs,time2,1);

            //If condition satisfies

            if(feof(sfs))

            {

                //Break

                break;

            }

            //Store

            fscanf(sfs,"%d",&time2);

        }

    }

    //Loop

    while(!feof(rfs))

    {

        //Call method

        process(rfs,time1,0);

        //If condition satisfies

        if(feof(rfs))

        {

            //Break

            break;

        }

        //Store value

        fscanf(rfs,"%d",&time1);

    }

    //Loop

    while(!feof(sfs))

    {

        //Call method

        process(sfs,time2,1);

        //If condition satisfies

        if(feof(sfs))

        {

            //Break

            break;

        }

        //Store value

        fscanf(sfs,"%d",&time2);

    }

    //Pause console window

    getchar();

    //Pause console window

    getchar();

    //Return

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote