1

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
Dean
  • 97
  • 1
  • 14
  • What is it that you are trying to do? are you trying to print each one on a new line? or are you trying to clear the current line and overwrite the line with the updated time? – Alex Zywicki Feb 10 '17 at 19:32
  • sorry, i should have been more specific, i am trying to clear the current line and overwrite – Dean Feb 10 '17 at 19:34
  • It's better to copy paste the output of your program directly into the question rather than uploading an Image to the output. I suggest you edit the question to do so. – Alex Zywicki Feb 10 '17 at 19:34
  • i have edited it now, is that better? – Dean Feb 10 '17 at 19:37
  • Format the output as code – Alex Zywicki Feb 10 '17 at 19:38
  • So you're trying to update the first line every second? – Toma Feb 10 '17 at 19:38
  • See Here: https://stackoverflow.com/questions/28350254/how-do-rewrite-a-line-of-text-in-a-console-project-c/28350285#28350285 – Alex Zywicki Feb 10 '17 at 19:39
  • And Here: https://stackoverflow.com/questions/1508490/how-can-i-erase-the-current-line-printed-on-console-in-c-i-am-working-on-a-lin – Alex Zywicki Feb 10 '17 at 19:39
  • yes thats what I am trying to do, thank you alex, I am kinda new to stack overflow – Dean Feb 10 '17 at 19:43
  • Possible duplicate of [How do rewrite a line of text in a console project? c++](http://stackoverflow.com/questions/28350254/how-do-rewrite-a-line-of-text-in-a-console-project-c) – Alex Zywicki Feb 10 '17 at 19:53

2 Answers2

1

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

manishg
  • 9,520
  • 1
  • 16
  • 19
  • I have already tried a similar approach and I have tried what you suggested but neither of them seemed to work – Dean Feb 11 '17 at 11:25
  • ah thats my problem, im using the Xcode console, I dont know how to send programs to terminal, ill look into that now, thank you for your help – Dean Feb 11 '17 at 16:09
  • Lets say the above program is in file test.cpp. Go to terminal and do "clang++ test.cpp" then you can just do ./a.out to run – manishg Feb 11 '17 at 17:20
  • Added images to explain this better – manishg Feb 12 '17 at 02:43
  • 1
    i tested it in the terminal there and it works perfect! that was driving me mad haha thank you so much, do u think its just a problem so with the xcode console?? – Dean Feb 12 '17 at 12:48
0

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?

Community
  • 1
  • 1
  • 1
    Or just use `std::endl`...but the OP is not trying to print a newline, the intent is to overwrite the text of the current line – Alex Zywicki Feb 10 '17 at 19:42
  • Understood, I assumed the new line was what he was going for but he was getting the wrong type but i now understand that was the actual output included above and not his desired result. – throwSmartDev Feb 10 '17 at 19:44