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

I need this C++ code converted to C code #include<iostream> #include <fstream> #

ID: 3726524 • Letter: I

Question

I need this C++ code converted to C code

#include<iostream>
#include <fstream>
#include <time.h>

using namespace std;

char *readableTime(long int sec) { // Converts seconds to time format
time_t epoch_time = (time_t)sec;// Convert to character format
return asctime(localtime(&epoch_time)); }

void readFile() {
ifstream rFile1, rFile2;
rFile1.open("Data1_in.txt");
rFile2.open("Data1_out.txt");
string data1, data2;
long int time1, time2;
string message1, message2;
string phoneNo1, phoneNo2;
rFile1>>time1;
rFile2>>time2;
rFile1>>phoneNo1;
rFile2>>phoneNo2;
data1 = readableTime(time1);
data2 = readableTime(time2);
getline(rFile1, message1);
getline(rFile2, message2);

while(!rFile1.eof() && !rFile2.eof()){
if(data1.compare(data2) < 0) {
cout<<" "<<data1<<" From "<<phoneNo1.substr(0, 3)<<"-"<<phoneNo1.substr(3, 3)<<"-"<<phoneNo1.substr(6)<<" | "<<message1.substr(3);
rFile1>>time1;
rFile1>>phoneNo1;
data1 = readableTime(time1);
getline(rFile1, message1); }

else {

cout<<" "<<data2<<" To "<<phoneNo2.substr(0, 3)<<"-"<<phoneNo2.substr(3, 3)<<"-"<<phoneNo2.substr(6)<<" | "<<message2.substr(2);
rFile2>>time2;
rFile2>>phoneNo2;
data2 = readableTime(time2);
getline(rFile2, message2); }
}

while(!rFile1.eof()){
cout<<" "<<data1<<" From "<<phoneNo1.substr(0, 3)<<"-"<<phoneNo1.substr(3, 3)<<"-"<<phoneNo1.substr(6)<<" | "<<message1.substr(3);
rFile1>>time1;
rFile1>>phoneNo1;
data1 = readableTime(time1);
getline(rFile1, message1); }

while(!rFile2.eof()) {
cout<<" "<<data2<<" To "<<phoneNo2.substr(0, 3)<<"-"<<phoneNo2.substr(3, 3)<<"-"<<phoneNo2.substr(6)<<" | "<<message2.substr(2);
rFile2>>time2;
rFile2>>phoneNo2;
data2 = readableTime(time2);
getline(rFile2, message2);}
rFile1.close();
rFile2.close();
}

int main(){
readFile(); }

Explanation / Answer

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

#include<ctype.h>

char *readableTime(long int sec) { // Converts seconds to time format

time_t epoch_time = (time_t)sec;// Convert to character format

return asctime(localtime(&epoch_time)); }

char* substring (char* input, int offset, int len, char dest[])

{

int input_len = strlen (input);

if (offset + len > input_len)

{

return NULL;

}

strncpy (dest, input + offset, len);

return dest;

}

void readFile() {

FILE *rFile1=fopen("Data1_in.txt","r");

//rFile1.open("Data1_in.txt");

FILE *rFile2=fopen("Data1_out.txt","r");

char data1[100];

char data2[100];

char pointer1[100],pointer2[100],pointer3[100],pointer4[100];

//string data1, data2;

long int time1, time2;

char message1[100], message2[100];

char phoneNo1[100], phoneNo2[100];

int len;

fscanf(rFile1,"%ld",&time1);

// data1 = readableTime(time1);

fscanf(rFile2,"%ld",&time2);

// data2 = readableTime(time2);

while((fscanf(rFile1,"%s",phoneNo1)!=-1) && (fscanf(rFile2,"%s",phoneNo2)!=-1)){

//fscanf(rFile1,"%[^ ]s",phoneNo1);

//fscanf(rFile2,"%[^ ]s",phoneNo2);

if(time1<time2) {

fscanf(rFile1,"%[^ ]s",message1);

substring(phoneNo1, 0, 3,pointer1);

substring(phoneNo1, 3, 3,pointer2);

substring(phoneNo1, 6,4,pointer3);

// substring(message1, 0, 3,pointer4);

printf(" %s From %s - %s - %s | %s",readableTime(time1),pointer1,pointer2,pointer3,message1 );

//rFile1>>time1;

fscanf(rFile1,"%ld",&time1);

}

else {

fscanf(rFile2,"%[^ ]s",message2);

substring(phoneNo2, 0, 3,pointer1);

substring(phoneNo2, 3, 3,pointer2);

substring(phoneNo2, 6,4,pointer3);

// substring(message2, 0, 2,pointer4);

printf(" %s To %s - %s - %s | %s",readableTime(time2),pointer1,pointer2,pointer3,message2 );

fscanf(rFile2,"%ld",&time2);

}

}

while((fscanf(rFile1,"%s",phoneNo1)!=-1)){

fscanf(rFile1,"%[^ ]s",message1);

substring(phoneNo1, 0, 3,pointer1);

substring(phoneNo1, 3, 3,pointer2);

substring(phoneNo1, 6,4,pointer3);

// substring(message1, 0, 3,pointer4);

printf(" %s From %s - %s - %s | %s",readableTime(time1),pointer1,pointer2,pointer3,message1 );

fscanf(rFile1,"%ld",&time1);

}

while((fscanf(rFile2,"%s",phoneNo2)!=-1)){

fscanf(rFile2,"%[^ ]s",message2);

substring(phoneNo2, 0, 3,pointer1);

substring(phoneNo2, 3, 3,pointer2);

substring(phoneNo2, 6,4,pointer3);

//substring(message2, 0, 3,pointer4);

printf(" %s To %s - %s - %s | %s",readableTime(time2),pointer1,pointer2,pointer3,message2 );

fscanf(rFile2,"%ld",&time2);

}

}

int main(){

readFile();

}

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