Author Topic: Typewriter output effect taking the piss  (Read 11400 times)

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Typewriter output effect taking the piss
« 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  :)

Code: [Select]

#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.

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Typewriter output effect taking the piss
« Reply #1 on: August 23, 2005, 01:19:07 PM »
Heres what I got:
Quote
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

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Typewriter output effect taking the piss
« Reply #2 on: August 23, 2005, 01:29:49 PM »
Weird I didn't get those warnings. Were you compiling with any switches other than -o ?

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Typewriter output effect taking the piss
« Reply #3 on: August 23, 2005, 01:36:04 PM »
Oh FFS it still doesn't work.

Code: [Select]

#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;
}

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
Typewriter output effect taking the piss
« Reply #4 on: August 23, 2005, 09:41:30 PM »
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
Code: [Select]

o
e
o
e
o
e
o
e
o
e
...

(o for stdout, e for stderr), it's more like this:
Code: [Select]

e
e
e
e
e
e
e
e
e
e
o
o
o
o
o
o
o
o
o
o
e
e
...

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Typewriter output effect taking the piss
« Reply #5 on: August 24, 2005, 12:48:37 AM »
Quote from: ahluka
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.

Code: [Select]
   for (i = 0; i < strlen(mesg); i++) {
        sleep (1);
        printf ("%c", mesg[i]);
        fflush(stdout);
    }

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
Typewriter output effect taking the piss
« Reply #6 on: August 24, 2005, 01:36:25 AM »
Quote from: Mike
Of course I am.  Turn on some warnings ( -Wall is a great one ).

Instead of using stderr you could also flush the buffer.

Code: [Select]
   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

sand_man

  • Super Jackass
  • Jackass II
  • Posts: 54
  • Karma: +10/-0
Typewriter output effect taking the piss
« Reply #7 on: August 24, 2005, 08:36:01 AM »
Quote from: ygfperson
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

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Typewriter output effect taking the piss
« Reply #8 on: August 24, 2005, 08:52:07 AM »
Quote from: Mike
Of course I am.  Turn on some warnings ( -Wall is a great one ).

Instead of using stderr you could also flush the buffer.

Code: [Select]
   for (i = 0; i < strlen(mesg); i++) {
        sleep (1);
        printf ("%c", mesg[i]);
        fflush(stdout);
    }


Thanks it works :D

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Typewriter output effect taking the piss
« Reply #9 on: August 24, 2005, 09:20:36 AM »
Quote from: ygfperson
Is that legal C? I thought fflush was only to be used for stdin

MY EYES!! MY EYES!! THEY BURN!!!!!

Quote

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)

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
Typewriter output effect taking the piss
« Reply #10 on: August 24, 2005, 05:14:38 PM »
haha

I guess I'll remember now