-1

Is there any way that I can fflush() a multi-line output? For example, if I wanted to print a 5x5 array in 5 separate lines and shuffling it multiple times every 1 second, how am I supposed to use fflush()?

#include <stdio.h>
#include <stdlib.h>
int main(){
    int arr[5][5]; //Just an array of numbers
    for(int k = 0; k < 10; k++){
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 5; j++){
                printf("%d ", arr[i][j]);
            }
            printf("\n");
        }
        fflush(stdout);
        shuffle(arr);
        sleep(1);
    }
    return 0;
}

This how i would want the output to change (without of course creating any additional new lines):

 0 1 2 3 4       5 3 12 7 8
 6 7 8 9 10  ->  1 2 0 13 6
     .               .
     .               .
     .               .

I tried both rewind(stdout) and fsetpos() but it didn't seem to do anything.

  • You invoked *undefined behavior* by using values of uninitialized non-static local varialbe `arr`, which are indeterminate, so anything is allowed to happen. – MikeCAT Feb 03 '21 at 16:37
  • Also `shuffle` is used without declaration. – MikeCAT Feb 03 '21 at 16:37
  • They do not serve any purpose, i am just giving an example @MikeCAT – Κωνσταντινος Χαφης Feb 03 '21 at 16:39
  • I do not understand. `fflush` flushes what already been `printf`-ed. It's not like you call `printf`, modify the values, and then `fflush` and it's going to print modified values. It's going to print the values at the time `printf` was called. – KamilCuk Feb 03 '21 at 16:41
  • 1
    You can call `fflush` in this way, however there is little point in doing so, it will have no effect in combination with the code given (stdout is usually line-buffered, outputting a `\n` will flush it). What problem are you actually trying to solve? Try describing the problem instead. –  Feb 03 '21 at 16:42
  • would adding a sleep help? yea i understand that fflushing will result in giving me the final output – Κωνσταντινος Χαφης Feb 03 '21 at 16:43
  • @ΚωνσταντινοςΧαφης I'm not really sure what the problem is, but sleep won't help that's for sure. – Jabberwocky Feb 03 '21 at 16:52
  • 1
    do you mean you want to **clear** the display? – Antti Haapala -- Слава Україні Feb 03 '21 at 16:55
  • @ΚωνσταντινοςΧαφης also rewind or fsetpos doesn't make any sense with stdout, at best it will do nothing. Once something has been sent to stdout with printf you cannot "take it back". But anyway I think this is an [XY Problem](https://xyproblem.info/), you need to tell us what you are _actually_ trying to achieve. Show us an example of desired output. – Jabberwocky Feb 03 '21 at 16:57
  • 1
    I think the code above should work, despite the fact that the fflush is superfluous and the values in the array are pretty arbitrary due to missing initialization. However, the memory is still allocated so access violation does not occur, in this case. You did not show the shuffle function. Maybe it's not returning from the call (e.g. endless loop) and you incorrectly suspected the printf's to output nothing. – Wör Du Schnaffzig Feb 03 '21 at 16:59
  • @Antti Haapala: That would explain the perplexity expressed in the comments. – Wör Du Schnaffzig Feb 03 '21 at 17:03
  • @ΚωνσταντινοςΧαφης Do you mean that you want the output of the next cycle to appear at the same screen position where the current output was displayed? Please [edit] your question to answer. Clearing the screen before printing the first or any subsequent array might be the easiest solution. Otherwise you might consider using `curses` (or `ncurses`). – Bodo Feb 03 '21 at 17:07
  • I think the real scope of above question in terms of the learning curve is 1.) Defining and initializing array, 2.) Printing array, 3.) Shuffling the array, 4.) Delaying and looping to repeat steps 1-3. The question, if the screen is cleared or not between prints is just a distraction from the original goal and a cosmetic issue. – Wör Du Schnaffzig Feb 03 '21 at 17:20

1 Answers1

0

What you want is called clearing the display. You can do system("cls"); on Windows or system("clear"); on Unixy systems.

Or there are https://en.wikipedia.org/wiki/ANSI_escape_code - if you print those special characters, stuff will happen, you can move the cursor to the top and overwrite the text or try just clearing the screen. I'm not confident which escape code will work for you, experiment.