I need to convert this to C code #include<iostream> #include<fstream> #include<c
ID: 3726718 • Letter: I
Question
I need to convert this to C code
#include<iostream>
#include<fstream>
#include<ctime>
using namespace std;
char* readableTime(long unsigned int sec){
time_t t = (time_t)sec;
return asctime(localtime(&t));
}
void process(ifstream &file,long unsigned int time,int n){
char *t = readableTime(time);
string display(t);
if(n == 0){
display[display.length()-1] = ' ';
display += " From ";
}
if(n == 1){
display[display.length()-1] = ' ';
display += " To ";
}
int wor = 0;
string phone;
file>>phone;
for(int i = 0;i<phone.length();i++){
if(i != 0 && i%3 == 0 && i<7){
display += "-";
}
display += phone[i];
}
display += " | ";
file>>wor;
char ch;
for(int i = 0;i<=wor;){
file.get(ch);
if(i != wor){
display += ch;
}
if(ch == ' ' || ch == ' '){
if(ch == ' ' && i != wor){
display += " | ";
}
i++;
}
}
cout<<" "<<display;
}
int main(int argc,char **argv){
long unsigned int time1,time2;
if(argc != 3){
cout<<" Not Enough Argument !";
return -1;
}
string recieve(argv[0]);
string sent(argv[1]);
ifstream rfs;
ifstream sfs;
rfs.open(recieve.c_str());
sfs.open(sent.c_str());
if(rfs.is_open() && sfs.is_open()){
}else{
cout<<" File Opening Error check files names ";
return -1;
}
rfs>>time1;
sfs>>time2;
while(!rfs.eof() && !sfs.eof()){
if(time1 < time2){
process(rfs,time1,0);
if(rfs.eof()){
break;
}
rfs>>time1;
}else{
process(sfs,time2,1);
if(sfs.eof()){
break;
}
sfs>>time2;
}
}
while(!rfs.eof()){
process(rfs,time1,0);
if(rfs.eof()){
break;
}
rfs>>time1;
}
while(!sfs.eof()){
process(sfs,time2,1);
if(sfs.eof()){
break;
}
sfs>>time2;
}
return 0;
}
Explanation / Answer
#
include < stdio.h > #include < stdlib.h > #include < time.h >
char * readableTime(long unsigned int sec) {
time_t t = (time_t) sec;
return asctime(localtime( & t));
}
void process(FILE *file, long unsigned int time, int n) {
char * t = readableTime(time);
int i=0;
display(t);
if (n == 0) {
display[sizeof(display) - 1] = ' ';
display += " From ";
}
if (n == 1) {
display[sizeof(display) - 1] = ' ';
display += " To ";
}
int wor = 0;
char phone[100];
file = open("file name", "r");
for ( i = 0; i < sizeof(phone); i++) {
if (i != 0 && i % 3 == 0 && i < 7) {
display += "-";
}
display += phone[i];
}
display += " | ";
file >> wor;
char ch;
for (i = 0; i <= wor;) {
file.getc(ch);
if (i != wor) {
display += ch;
}
if (ch == ' ' || ch == ' ') {
if (ch == ' ' && i != wor) {
display += " | ";
}
i++;
}
}
printf( " %s", display);
}
int main(int argc, char * * argv) {
long unsigned int time1, time2;
if (argc != 3) {
printf(" Not Enough Argument !");
return -1;
}
char recieve[100] = argv[0];
char sent[100] = argv[1]);
FILE *recptr, sentptr;
char ch1, ch2;
recptr.open(recieve);
senptr.open(sent);
if (recptr==NULL && sentptr==NULL) {} else {
printf(" File Opening Error check files names ");
return -1;
}
fprintf(recptr, "%lu", time1);
fprintf(sentptr, "%lu", time2);
ch1 = fgetc(recptr);
ch2 = fgetc(sentptr);
while (ch1!=EOF && ch2!=EOF) {
if (time1 < time2) {
process(recptr, time1, 0);
if (ch1==EOF) {
break;
}
fprintf(recptr,"%lu",time1);
} else {
process(senptr, time2, 1);
if (ch2==EOF) {
break;
}
fprintf(senptr, "%lu",time2);
}
}
while (ch2!=EOF) {
process(sentptr, time1, 0);
if (ch2==EOF) {
break;
}
fprintf(sentptr,"%lu", time1);
}
while (ch2!=EOF) {
process(sentptr, time2, 1);
if (ch2==EOF)) {
break;
}
fprintf(sentptr, "%lu",time2);
}
return 0;
}
//Please mention input and output file format i will test the code and update
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.