Dean
Dean

Reputation: 97

carriage return not working in xcode console c++

I want to output the time and loop it so the time is constantly being updated without outputting the same line again and again so I thought id overwrite the line by using the carriage but it doesn't seem to work and I don't know why, is it to do with Xcode?? I have previously read other posts on stack overflow regarding \r and tried to adapt the answers to my code but none of the solutions seemed to work nor are they related to ios/Xcode. I am also assuming that the problem may have something to do with the Xcode console, being as how it is not a terminal (however I am not to sure)

#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, const char * argv[]) {
    //Loop Forever
    for(;;){
        time_t rawtime;
        struct tm * timeinfo;
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        cout << "The current date/time is: " << asctime(timeinfo) << "\r";
    }
    return 0;
}


output:

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

The current date/time is: Fri Feb 10 17:20:43 2017

Upvotes: 1

Views: 1847

Answers (2)

manishg
manishg

Reputation: 9828

The problem you have is due to this function asctime(timeinfo). If you dump the hex output of the output of this function, it contains '\n'. Here is an example

467269204665622031302032333A33343A303620323031370A

Please notice the last 2 characters. It is 0x0A which means it has '\n' at the end of the string. So whatever you try it won't fix the issue till you fix this string. You need to delete \n character from the string and then adding '\r' will suffice your need. Here is my solution (I am mixing C code in C++ as asctime() returns char *). You can find alternate solutions.

#include <iostream>
#include <time.h>
using namespace std;

void remove_all_chars(char* str, char c) {
    char *pr = str, *pw = str;
    while (*pr) {
        *pw = *pr++;
        pw += (*pw != c);
    }
    *pw = '\0';
}

int main(int argc, const char * argv[]) {
    //Loop Forever
    for(;;){
        time_t rawtime;
        struct tm * timeinfo;
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        char *timeString = asctime(timeinfo);
        remove_all_chars(timeString, '\n');
        cout << "The current date/time is: " << timeString << "\r";
    }
    return 0;
}

With original code:

enter image description here

With my modified code

enter image description here

Upvotes: 1

throwSmartDev
throwSmartDev

Reputation: 45

Sometimes to get the correct return you need to use the \r\n and when using escape characters I believe you need double quotes and not single quotes or it is rendered as a literal. Make sure you are using the proper return type for your OS - \r\n , \r , \n what is the difference between them?

Upvotes: 0

Related Questions