【问题标题】:carriage return not working in xcode console c++回车在xcode控制台c ++中不起作用
【发布时间】:2017-06-29 06:17:47
【问题描述】:

我想输出时间并循环它,这样时间会不断更新,而不会一次又一次地输出同一行,所以我认为 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

【问题讨论】:

  • 你想做什么?您是否要在新行上打印每个?还是您要清除当前行并用更新的时间覆盖该行?
  • 对不起,我应该更具体,我正在尝试清除当前行并覆盖
  • 最好将程序的输出直接复制粘贴到问题中,而不是将图像上传到输出中。我建议您编辑问题以这样做。
  • 我已经编辑了,这样更好吗?
  • 将输出格式化为代码

标签: c++ ios xcode


【解决方案1】:

您遇到的问题是由于此功能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;
}

原代码:

使用我修改后的代码

【讨论】:

  • 我已经尝试过类似的方法,并且我已经尝试了您的建议,但它们似乎都不起作用
  • 啊,这是我的问题,我使用的是 Xcode 控制台,我不知道如何将程序发送到终端,现在正在研究,谢谢您的帮助
  • 假设上面的程序在文件 test.cpp 中。转到终端并执行“clang++ test.cpp”然后您可以执行 ./a.out 来运行
  • 添加图片以更好地解释这一点
  • 我在那里的终端上对其进行了测试,效果很好!这让我发疯了哈哈非常感谢你,你认为这只是 xcode 控制台的问题吗??
【解决方案2】:

有时要获得正确的返回,您需要使用 \r\n 并且在使用转义字符时,我相信您需要双引号而不是单引号,否则它会呈现为文字。确保您为您的操作系统使用正确的返回类型 - \r\n , \r , \n what is the difference between them?

【讨论】:

  • 或者只是使用std::endl...但是OP没有尝试打印换行符,目的是覆盖当前行的文本
  • 明白,我认为新行是他想要的,但他的类型错误,但我现在明白这是上面包含的实际输出,而不是他想要的结果。
猜你喜欢
  • 2022-06-24
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多