20

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);
}
};
4

8 に答える 8

26

キャリッジリターンを探しています。C では、それは\r. これにより、新しい行 (改行) を開始せずに、カーソルが現在の行の先頭に戻ります。

于 2013-03-03T23:59:23.293 に答える
6

「\n」の代わりに「\r」を使用して実行できます。

于 2013-03-04T00:01:08.383 に答える
3

どこに印刷?

データを標準出力に出力している場合、通常、戻って既に書き込まれたものを変更することはできません。\r標準出力が端末に向けられている場合、一部の端末ではカーソルを行の先頭に移動する文字を出力してみることができます (効果はプラットフォームに依存します)。前の行。これにより、古いデータが新しいデータに置き換えられたという視覚効果が生まれます。ただし、これはストリーム内の古いデータを実際に「置き換える」わけではありません。つまり、標準出力をファイルにリダイレクトすると、出力されたすべてのデータがファイルに保存されます。\r端末の行全体を強制的に上書きすることに注意してください。

データをファイルに出力する場合、fseek関数を使用して以前に訪れたポイントに戻り、そこから「最初からやり直す」ことができ、その過程でデータを上書きできます。

于 2013-03-04T00:03:03.907 に答える
1

「\b」文字 (バックスペース) をテストしましたか? コンソールによっては機能するかもしれません。

于 2013-03-03T23:59:58.393 に答える
0

コンソール画面と同じ数の改行を出力できます。これにより、画面が効果的にクリアされます。

これは、さまざまな方法で画面をクリアするための優れたリンクです。

于 2013-03-03T23:57:11.940 に答える
-1

理解するには、サンプル コードを参照してください。

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

参考ブログ

于 2015-09-07T10:27:56.550 に答える