Er........wat is the command for me to display the date of today, according to the system date onto the screen display?
Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?
Er........wat is the command for me to display the date of today, according to the system date onto the screen display?
Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?
Only by the cross are you saved...
>wat is the command for me to display the date of today
The simplest is:
>which isl ike a clock, whereby the time is updated real-time each second?Code:#include <stdio.h> #include <time.h> int main(void) { time_t today; if ((today = time(0)) != (time_t)-1) printf("%s", ctime(&today)); return 0; }
Yes, here is the simplest way (which is utterly useless, so I didn't bother making it nice):
To have an interactive clock like this you need to either spawn another process, or make use of multithreading. Otherwise the program will run and all you will be able to do is look at it and do nothing else.Code:#include <stdio.h> #include <time.h> int main(void) { time_t hereandnow; struct tm *today; char buff[BUFSIZ]; while (1) { hereandnow = time(0); today = localtime(&hereandnow); strftime(buff, BUFSIZ, "%I:%M:%S", today); printf("%s\r", buff); fflush(stdout); } return 0; }
My best code is written with the delete key.
I have 3 questions about your code:
1) what is the condition of the while statement? It never stops.Code:while (1) { hereandnow = time(0); today = localtime(&hereandnow); strftime(buff, BUFSIZ, "%I:%M:%S", today); printf("%s\r", buff); fflush(stdout); }
2) What %I, %M, %S represent?
3) What's the use of fflush(stdout) ?
Thanks in advance
It should be interrupted from the keyboard(Ctrl-c, for example).1) what is the condition of the while statement? It never stops.
The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.3) What's the use of fflush(stdout) ?
Well, you asked "Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?". The only way to update every second is to run. If you want a way out, you have to add code that tells the loop when to break;Originally posted by AProg
I have 3 questions about your code:
1) what is the condition of the while statement? It never stops.
Look up the function. The documentation explains all.2) What %I, %M, %S represent?
Look up the function. The documentation explains all.3) What's the use of fflush(stdout) ?
Try Googling for your answers
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.
[italic]int fflush(FILE *stream);
Flushes a stream.
[/italic]
I searched a lot about the above discription but my work established fruitless. You see, English is not my mother tonque, so i have some difficulties in understanding what "flush" means. I turned that word up in my dictionnary, but the tranlslated word didn't help me so much to understand what ffluch() does.
Would you mind explaining me the purpose of fflush in other words pls?
>Would you mind explaining me the purpose of fflush in other words pls?
The input buffer is like filling a pail of water*. Most of the time it's useful to fill the pail to the top before you pour it out, you get more water for your work that way. But, there are times where you don't want a full pail, you just fill it a bit and the pour out what you have, you're calling fflush on the pail.
* Input stream, get it?
My best code is written with the delete key.
Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?
>Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?
What if you want to water a little plant and a full pail is too much? Sure, you could fill it up and use a little, but then you would have left over water and need to pour it out elsewhere (what a waste). Sure, you could save it, but then you wouldn't have a pail to use for other things.
My best code is written with the delete key.
It's ok with the tail, but how is that relevant to the fflush.
Could you say: "Flushes a stream" whithout using the world "flushes" and not changing the meaning?
>"Flushes a stream" whithout using the world "flushes" and not changing the meaning?
I haven't changed the meaning once, "flush the stream" means "empty the contents". In my analogy, the pail is the buffer, when you empty the pail, you are flushing the buffer. It really isn't that difficult.
My best code is written with the delete key.
>"empty the contents"
Sorry for asking again, but what contents do you refer to?
>Sorry for asking again, but what contents do you refer to?
The contents of the buffer. The following may not write directly to stdout:
printf("Hello, world!");
Because there's no newline character printed and no call to fflush, the string may be placed in a temporary buffer because calling a low level write function to place the string directly onto stdout can be expensive. When the buffer is full, then it is written as a whole to stdout. You can cause this buffer to be emptied prematurely by placing a newline at the end of the string:
printf("Hello, world!\n");
or calling fflush on stdout:
printf("Hello, world!");
fflush(stdout);
My best code is written with the delete key.
>printf("Hello, world!");
>fflush(stdout);
So, in this example, it would be the same if i placed " printf( "\n" ); ", istead of " fflush( stdout ); " ?
no because you wouldn't get a newline with fflush.