Update printf value on same line instead of new

I would like to know if there is a way in C overwrite an existing value that has already been printed, instead of creating a new line each time or just navigating a space. I need to get real-time data from the sensor, and I would like to just sit there and constantly update existing values โ€‹โ€‹without scrolling. Is it possible?

UPDATE: ADDED CODE

 #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); } }; 
+8
c
source share
7 answers

You can print as many lines as the console screen has. This will effectively clear the screen.

This is a great link about cleaning the screen in different ways.

+1
source share

You are looking for a carriage return. In C, it's \r . This will return the cursor to the beginning of the current line without starting a new line (linefeed)

+20
source share

You should add \r to your printf, as others have said. Also, be sure to flush stdout because the stdout stream is buffered and only displays what is in the buffer after it reaches a new line.

In your case:

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

You can do this using "\ r" instead of "\ n".

+4
source share

Printed where?

If you output data to standard output, you cannot even go back and change everything that has already been written. If your standard output is directed to a terminal, you can try outputting the \r character, which will move the cursor to the beginning of a line on some terminals (the effect is platform dependent), so that the next output line will overwrite what was previously printed on that line. This will have the visual effect of replacing old data with new data. However, this does not "replace" the old data in the stream, which means that if you redirect standard output to a file, the file will save everything that was printed. Keep in mind that \r will force you to overwrite the entire line on the terminal.

If you output your data to a file, you can use the fseek function to return to some previously visited point and โ€œstart from itโ€, overwriting the data in the process.

+2
source share

Have you tested the character '\ b' (backspace)? It may work depending on your console.

+1
source share

Refer to the sample code to understand:

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

Help blog

0
source share

All Articles