更新同一行上的printf值而不是新行

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

更新:增加的代码

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

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

在你的情况下:

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

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

您可以使用“\ r”而不是“\ n”来完成此操作。

印在哪里?

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

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

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

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

你测试了’\ b’字符(退格)吗? 也许取决于你的控制台。

请参阅示例代码以了解:

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

参考博客

除了上面的答案,\ r \ n实际上是终端的代码。 c似乎没有提供一种方法来改变已经放入stdout流的程序。

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

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

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

 #include #include #include 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