Sleep or Dealy fucntion Issue

I am doing some programming using C on UNIX. The thing is, I have been working in both UNIX and Windows for practicing.

I created a Delay fucntion over Windows using DevC++ as a compiler, when I tried over the console, it worked as I expected, I mean it was creating a delay (of one second for example) between each operation.

When I tried this function over UNIX, it didn't get the same results. I would like to know what is happening. Then, I tried a Sleep function from the <unistd.h> library and it did the same. When I tried to do an operation that will be printed in the same line, the delay used between each operation is acummulated and then everything is done. For example;

Sleep(1); printf("Hi"); Sleep(1); printf("How"); Sleep(1); printf("are"); Sleep(1); printf("you?");

I have to wait four seconds to get all the expression to be printed in the screen, instead of getting each expresion with a one second delay. I hope somebody can help me out. I will aprecciate any help.

Other thing, I have been using the conio.h library of windows to put some color on the console. What can I used on UNIX? Can anybody give a working example?

  1. try
sleep(1);
printf("Hi");
fflush(stdout);
sleep(1);
printf("How");
fflush(stdout);
sleep(1); 
printf("are"); 
fflush(stdout);
sleep(1); 
printf("you?"); 
fflush(stdout);

the problem you describe is caused by stdio doing buffering.

  1. look to curses/ncurses to provide terminal independent fancy output else encode VT100 escape sequences yourself.

Ok I am going to try the code you are telling me. It is wear how different works the stdio in Windows so.

Can you help out with the curses.h library? I just want to put some color and maybe some flashing text.. that's all..

They are (a) different operating systems and (b) have different implementations of the C library.

http://www.comptechdoc.org/os/linux/howlinuxworks/linux_hlvt100.html

I want the details for UNIX. Either KDE 3.5 or KNOPPIX 5.1. By the way, I tried what you told me and it work very good!

How do I use the VT100 ESC Sequences?

Thanks!

That's why we are here.

Then surely X-Windows would be the route to go.

Embed them in the character stream you write to the terminal..

printf("\033[5m");

Let me understand something. If I want to put some color to for example:

printf("\033[5m"); printf("Blue");

printf("\033[5m"); printf("Green");

Is it going to be this way? or everything together?

VT100 was monochrome, try this

http://www.cs.utk.edu/~shuford/terminal/vt241_color_news.txt

That would indeed be flashing "Blue", but not what I think you intend. Yes, you can run the escape codes together, the consoles receive them as a stream anyway.

Basically UNIX is Bufferred system.
It has two types of buffering system.
1) Line Buffering
2) Block Buffering.

The printf() in C uses the Linf Buffering System. It means, first the Strings are moved to a Buffer. After the Buffer moves the strings to console/memory. The Buffer is cleared, when any one of the following scenario occurs.
1) If the Buffer is full
2) new line Char "\n" occurs
3) The Buffer is flushed by fflush()
4) The Program Terminates.

from the porter's Solution
we have to use the fflush to clear the buffer.
and also we have to use "\n" in all the printf().

Ex:
printf("Hi\n");

Not both really,
either use fflush or "\n" in printf