-1

I want to create a program to print out the first printf statement, Then in the next line, it will clear the first printf statement and print the next statement. Please help me fix the code.

printf("Please wait while Loading...");
Sleep(2132);
printf("Done Loading");
6
  • 1
    C only knows input and output streams, you can't modify what you already output! So, to achieve something like this, you'll need to use OS-specific APIs or a library abstracting them (curses comes to mind). First think about whether you really need this.
    – user2371524
    Sep 28, 2017 at 10:16
  • What is the problem with your current code? What goes wrong?
    – rustyx
    Sep 28, 2017 at 10:17
  • use return carriage. \r
    – Wreigh
    Sep 28, 2017 at 10:18
  • @Wreigh this depends on the output device, so I wouldn't advice this.
    – user2371524
    Sep 28, 2017 at 10:19
  • 1
    Ok, revising my comments: C indeed specifies \r, so if the output device is capable to jump to the beginning of the same line, this must happen on \r. Probably good enough for this scenario!
    – user2371524
    Sep 28, 2017 at 10:23

4 Answers 4

2

This should work:

printf("Please wait while Loading...");
fflush(stdout);    // flush output, this is necessary on some platforms,
                  // otherwise the text won't be printed immediately
Sleep(2132);
printf("\rDone Loading               \n");  // the \r returns to the start of the line, 
                                            // and the trailing spaces are necessary to
                                            // erase the remaining text
0
1

Assuming stdout is a terminal or window that supports overwriting of text, then the simple solution is to output a '\r' (carriage return), overwrite with spaces, and then print the second string.

printf("Please wait while Loading...");
fflush(stdout);
Sleep(2132);
printf("\r");
  /*  output the number of space characters equal to length of the preceding string */
printf("\r");
printf("Done Loading");
fflush(stdout);

There is the problem that not all terminals/windows support this correctly. In that case, you'll need to use techniques specific to the terminal/window and host system. This approach may also not work if standard output has been redirected to a file or pipe.

1

This problem is not specific to C (or any other language for that matter), but to the console that you're printing to.

Many consoles understand the carriage return (\r) character to move the cursor to the beginning of the current line. You can try using that and "erase" the last message with spaces:

printf("Please wait while Loading...");
Sleep(1000);
printf("\r                            ");
printf("\rDone Loading\n");
1

Pedantically you can't to what you want at least not in portable C99 or C11. BTW sleep is not standard C (but in POSIX). You probably want some operating system specific stuff. I hope your are using Linux.

The stdio (i.e. <stdio.h> from standard C) only defines a limited notion of standard streams and standard output, much simpler than terminal emulators.

(for example on Linux your program might be used in some command pipeline , something like yourprogram | grep foo | less then what you want is meaningless).

If you want to write a terminal-based application with a text-based interface, use a library for that. On Linux and POSIX it would be ncurses. Or you could use some ANSI escape codes and/or in some limited cases the return character \r. Remember that stdio is buffered, so you might need fflush(3). Read the tty demystified and termios(3).

You might even want to code a graphical user interface. This should be thought at design phase (because your program needs some event loop so would have a different architecture). Then use a widget toolkit like e.g. GTK.

2
  • 1
    The behaviour of \r is documented in the standard though, though overwriting has undefined behaviour by omission ( :) ) presumably so that a line printer can be supported for the stdout Sep 28, 2017 at 10:35
  • But if you run on Linux someprogramwritingcarriagereturn | less you often would be disappointed Sep 28, 2017 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.