c++ - 回车在 xcode 控制台 c++ 中不起作用

标签 c++ ios xcode

我想输出时间并循环它,这样时间就会不断更新,而不会一次又一次地输出同一行,所以我认为 id 通过使用回车来覆盖该行,但它似乎不起作用,我不这样做不知道为什么,是不是跟Xcode有关??我之前阅读过有关\r 的堆栈溢出的其他帖子,并尝试将答案调整为我的代码,但这些解决方案似乎都不起作用,也与 ios/Xcode 无关。我还假设问题可能与 Xcode 控制台有关,因为它不是终端(但我不确定)

#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

最佳答案

您遇到的问题是由于此函数 asctime(timeinfo) 引起的。如果转储此函数输出的十六进制输出,它包含 '\n'。这是一个例子

467269204665622031302032333A33343A303620323031370A

请注意最后 2 个字符。它是 0x0A,这意味着它在字符串末尾有 '\n'。因此,无论您尝试什么,在您修复此字符串之前都无法解决问题。您需要从字符串中删除 \n 字符,然后添加 '\r' 就可以满足您的需要。这是我的解决方案(我在 C++ 中混合 C 代码,因为 asctime() 返回 char *)。您可以找到替代解决方案。

#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;
}

原代码:

enter image description here

用我修改过的代码

enter image description here

关于c++ - 回车在 xcode 控制台 c++ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42167188/

相关文章:

C++ windows32 winsock UDP路由?

php - 使用 PHP 下载 JSON 格式的图像数据(blob)

iphone - 如何在 iPhone/iPad 上存储非常大的图像

objective-c - 视网膜显示器的矩形尺寸是多少?

ios - UITableview 滚动顶部然后执行操作方法

c++ - 在位置 N 处检索 C++ 可变参数模板常量参数值的适当方法是什么?

c++ - 字符串作为 C++ 中宏的参数

c++ - 如何从 vector<Rect> 中删除重复矩形?

objective-c - UITabBarController 中的 View 在内存警告后消失

objective-c - 属性的合成 getter 遵循返回 'owned' 对象的 cocoa 命名约定