【问题标题】:Update printf value on same line instead of new one在同一行更新 printf 值而不是新的
【发布时间】:2013-03-03 23:56:09
【问题描述】:

我想知道C 中是否有一种方法可以覆盖已经打印的现有值,而不是每次都创建一个新行或只是移动一个空格。我需要从传感器获取实时数据,并希望它坐在那里并不断更新现有值而无需任何滚动。这可能吗?

更新:添加代码

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23


int fd;
short x = 0;
short y = 0;
short z = 0;
int main (){



    fd = wiringPiI2CSetup(0x69); // I2C address of gyro
    wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down
    wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal
    wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec)
    delay(200);                    // Wait to synchronize

void getGyroValues (){
    int MSB, LSB;

    LSB = wiringPiI2CReadReg8(fd, 0x28);
    MSB = wiringPiI2CReadReg8(fd, 0x29);
    x = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2B);
    LSB = wiringPiI2CReadReg8(fd, 0x2A);
    y = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2D);
    LSB = wiringPiI2CReadReg8(fd, 0x2C);
    z = ((MSB << 8) | LSB);
}
    for (int i=0;i<10;i++){
    getGyroValues();
    // In following Divinding by 114 reduces noise
    printf("Value of X is: %d\r", x/114);
//  printf("Value of Y is: %d", y/114);
//  printf("Value of Z is: %d\r", z/114);
    int t = wiringPiI2CReadReg8(fd, 0x26);
    t = (t*1.8)+32;//convert Celcius to Fareinheit
    int a = wiringPiI2CReadReg8(fd,0x2B);
    int b = wiringPiI2CReadReg8(fd,0x2A);
//  printf("Y_L equals: %d\r", a);
//  printf("Y_H equals: %d\r", b);
    int c = wiringPiI2CReadReg8(fd,0x28);
    int d = wiringPiI2CReadReg8(fd,0x29);
//  printf("X_L equals: %d\r", c);
//  printf("X_H equals: %d\r", d);
    int e = wiringPiI2CReadReg8(fd,0x2C);
    int f = wiringPiI2CReadReg8(fd,0x2D);
//  printf("Z_L equals: %d\r", e);
//  printf("Z_H equals: %d\r", f); 

//  printf("The temperature is: %d\r", t); 
    delay(2000);
}
};

【问题讨论】:

    标签: c


    【解决方案1】:

    您应该像其他人所说的那样将\r 添加到您的 printf 中。 此外,请确保刷新标准输出,因为标准输出流已缓冲,并且只会在到达换行符后显示缓冲区中的内容。

    在你的情况下:

    for (int i=0;i<10;i++){
        //...
        printf("\rValue of X is: %d", x/114);
        fflush(stdout);
        //...
    }
    

    【讨论】:

    • fflush 是一个关键注释,因为在大多数平台上,没有它就无法工作。
    • 当尝试使用 Xcode 9 的控制台(可能还有一般的 OS X)登录时,即使这个示例也不起作用。
    【解决方案2】:

    您正在寻找回车。在 C 中,这是\r。这将使光标回到当前行的开头而不开始新行(换行)

    【讨论】:

    • 一个有趣的问题是,如果要打印的新行比之前打印的行短。在这种情况下,屏幕上还会显示上一行的某些部分。
    • @Ganesh 然后为每个字符打印一个额外的空格。
    • @Armin 是的,这是一个解决方案。程序必须知道字符串长度,然后进行一些数学运算以添加空格以使其看起来正确。挑战在于是否有任何其他方法可以有效地flush 这条线。
    • 我已经更新了我的问题并添加了代码我在使用\r 时没有得到任何类型的输出我已经评论了除了for 循环内的所有其他行,但我会得到是一个闪烁的光标
    • @Yamaha32088 尝试将\r 放在printf 字符串的开头而不是结尾。回车可能会擦除终端上的行。
    【解决方案3】:

    您可以使用“\r”而不是“\n”来做到这一点。

    【讨论】:

      【解决方案4】:

      印在哪里?

      如果您将数据输出到标准输出,您通常无法返回并更改已写入的任何内容。如果您的标准输出被定向到终端,您可以尝试在某些终端上输出\r 字符,该字符会将光标移动到行首(效果取决于平台),以便后续输出行将覆盖原来的内容之前在该行中打印。这将产生旧数据被新数据替换的视觉效果。但是,这并没有真正“替换”流中的旧数据,这意味着如果您将标准输出重定向到文件,该文件将存储打印的所有内容。再次记住,\r 将强制您覆盖终端上的整行。

      如果您将数据输出到文件,那么您可以使用fseek 函数返回到某个先前访问过的点并从那里“重新开始”,覆盖该过程中的数据。

      【讨论】:

        【解决方案5】:

        您是否测试过“\b”字符(退格)?可能取决于您的控制台。

        【讨论】:

          【解决方案6】:

          您可以打印出与控制台屏幕一样多的换行符。这将有效地清除屏幕。

          这是一个很棒的link,关于以不同方式清除屏幕。

          【讨论】:

            【解决方案7】:

            参考示例代码了解:

            #include <stdio.h>
            #include <pthread.h>
            
            void myThread(void* ptr) {
                printf("Hello in thread\n");
                int i=0;    
                for(;i<10;i++)
                {
                    sleep(1);
                    printf(". ");
                    fflush(stdout);  //comment this, to see the difference in O/P
                }
                printf("sleep over now\n");
            }
            
            int main(void) {
                pthread_t tid;
                printf("creating a new thread\n");
                pthread_create(&tid, NULL, (void*)myThread, 0);
                printf("going to join with child thread..\n");
                pthread_join(tid, NULL);
                printf("joined..!!\n");
                return 0;
            }
            

            Reference Blog

            【讨论】:

              【解决方案8】:

              除了上面的答案,\r实际上是终端的代码。 c 似乎没有提供一种方法来更改已经放入标准输出流的任何程序。

              #include<stdio.h>
              int main(){
                  freopen("output.txt", "w", stdout);
                  printf("somthing");
                  printf("\r other thing");
              }
              

              在 output.txt 中,有些东西没有改变,因为 \r 对于 txt 文件没有任何意义。 但是对于终端来说,\r 是有意义的。它处理格式和显示良好。

              使用终端代码可以做一些有趣的事情。如下所示

              #include<iostream>
              #include<bits/stdc++.h>
              #include<unistd.h>
              
              int main(){
              std::string processBar[] {
                  "00%: [                     ]",
                  "05%: [#                    ]",
                  "10%: [##                   ]",
                  "15%: [###                  ]",
                  "20%: [####                 ]",
                  "25%: [#####                ]",
                  "30%: [######               ]",
                  "35%: [#######              ]",
                  "40%: [########             ]",
                  "45%: [#########            ]",
                  "50%: [##########           ]",
                  "55%: [###########          ]",
                  "60%: [############         ]",
                  "65%: [#############        ]",
                  "70%: [##############       ]",
                  "75%: [###############      ]",
                  "80%: [################     ]",
                  "85%: [#################    ]",
                  "90%: [###################  ]",
                  "95%: [#################### ]",
                  "100%:[#####################]",
              };
              
              int n = sizeof(processBar)/ sizeof(*processBar);
              // pretty fanny
              for(int i{0}; i<n; ++i){
                  fprintf(stdout, "\e[%d;1H \e[2K \r \a%s", i, processBar[i].c_str());
                  fflush(stdout);
                  sleep(1);
              }
              
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-03-26
                • 1970-01-01
                • 2010-11-26
                • 1970-01-01
                • 2019-10-30
                • 1970-01-01
                相关资源
                最近更新 更多