Update printf value on same line instead of new one

后端 未结 8 1178
刺人心
刺人心 2020-12-06 01:25

I would like to know if there is a way in C to overwrite an existing value that has already been printed instead of creating a new line every time or just movin

相关标签:
8条回答
  • 2020-12-06 01:51

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

    Reference Blog

    0 讨论(0)
  • 2020-12-06 01:57

    Have you tested the '\b' character (backspace)? Maybe works depending on your console.

    0 讨论(0)
  • 2020-12-06 02:00

    You can do it by using "\r" instead of "\n".

    0 讨论(0)
  • 2020-12-06 02:01

    You should add \r to your printf as others have said. Also, make sure you flush stdout, as stdout stream is buffered & will only display what's in the buffer after it reaches a newline.

    In your case:

    for (int i=0;i<10;i++){
        //...
        printf("\rValue of X is: %d", x/114);
        fflush(stdout);
        //...
    }
    
    0 讨论(0)
  • 2020-12-06 02:10

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

    This is a great link about clearing screen in different ways.

    0 讨论(0)
  • 2020-12-06 02:11

    In addition to answers above, \r is actually a code of the terminal. c seems don't provide a way to change whatever program has already put into stdout stream.

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

    in output.txt, something doesn't change, cause \r doesn't mean anything for a txt file. But for the terminal, \r is meaningful. It handles format and displays well.

    Using terminal codes could do some fun things. like below

    #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);
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题