EntropySink
Technical & Scientific => Programming => Topic started by: ahluka on August 23, 2005, 01:06:29 PM
-
I don't understand why this isn't working, and it's really starting to piss me off. It's probably something really obvious but give me a break, it's late and I've had a long hard day :)
#include
#include
int main ()
{
char mesg[] = "hello YOU WITH THE FACE";
int i;
for (i = 0; i < strlen (mesg); i++) {
sleep (40);
printf ("%c", mesg[i]);
}
return 0;
}
It compiles fine but when I run it, it just hangs and never prints the message.
I'm compiling with GCC under Linux, using the bash shell.
-
Heres what I got:
bt18.c: In function `main':
bt18.c:9: warning: implicit declaration of function `strlen'
bt18.c:10: warning: implicit declaration of function `sleep'
Now using man I found a sleep function defined in unistd.h however that takes in the number of seconds to sleep. I doubt you wish to sleep for 40 seconds between characters
-
Weird I didn't get those warnings. Were you compiling with any switches other than -o ?
-
Oh FFS it still doesn't work.
#include
#include
#include
int main ()
{
char mesg[] = "Heeeeeeeeeelo";
int i;
for (i = 0; i < strlen(mesg); i++) {
sleep (1);
printf ("%c", mesg[i]);
}
return 0;
}
-
stdout output is spooled and buffered to come out at a convienent time. Use stderr to get an immediate message.
//edit: to see what i'm talking about, write a loop which outputs one letter to stdout and then one letter to stderr per iteration. If you output to a file and read it later, you'll notice that instead of it being
o
e
o
e
o
e
o
e
o
e
...
(o for stdout, e for stderr), it's more like this:
e
e
e
e
e
e
e
e
e
e
o
o
o
o
o
o
o
o
o
o
e
e
...
-
Weird I didn't get those warnings. Were you compiling with any switches other than -o ?
Of course I am. Turn on some warnings ( -Wall is a great one ).
Instead of using stderr you could also flush the buffer.
for (i = 0; i < strlen(mesg); i++) {
sleep (1);
printf ("%c", mesg[i]);
fflush(stdout);
}
-
Of course I am. Turn on some warnings ( -Wall is a great one ).
Instead of using stderr you could also flush the buffer.
for (i = 0; i < strlen(mesg); i++) {
sleep (1);
printf ("%c", mesg[i]);
fflush(stdout);
}
Is that legal C? I thought fflush was only to be used for stdin
-
Is that legal C? I thought fflush was only to be used for stdin
No it's the other way around.
fflush(stdin) is undefined
-
Of course I am. Turn on some warnings ( -Wall is a great one ).
Instead of using stderr you could also flush the buffer.
for (i = 0; i < strlen(mesg); i++) {
sleep (1);
printf ("%c", mesg[i]);
fflush(stdout);
}
Thanks it works :D
-
Is that legal C? I thought fflush was only to be used for stdin
MY EYES!! MY EYES!! THEY BURN!!!!!
NAME
fflush - flush a stream
SYNOPSIS
#include
int fflush(FILE *stream);
DESCRIPTION
The function fflush forces a write of all user-space
buffered data for the given output or update stream via
the stream's underlying write function. The open status
of the stream is unaffected.
If the stream argument is NULL, fflush flushes all open
output streams.
RETURN VALUE
Upon successful completion 0 is returned. Otherwise, EOF
is returned and the global variable errno is set to indi-
cate the error.
ERRORS
EBADF Stream is not an open stream, or is not open for
writing.
The function fflush may also fail and set errno for any of
the errors specified for the routine write(2).
NOTES
Note that fflush() only flushes the user space buffers
provided by the C library. To ensure that the data is
physically stored on disk the kernel buffers must be
flushed too, e.g. with sync(2) or fsync(2).
CONFORMING TO
The fflush function conforms to ANSI X3.159-1989 (``ANSI
C'').
SEE ALSO
write(2), fclose(3), fopen(3), fsync(2), sync(2),
write(2), setbuf(3)
-
haha
I guess I'll remember now