0

I want my program to print a given file but instead of printing it at once I want the characters to appear with a certain interval.

Instead of waiting, for example, 1 second and then printing the character, it seems to wait 1 sec for every character it has to print in the current line and then prints all the characters at once.

I've tried using usleep() and sleep(), before and after the putchar() function, albeit with no success.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    FILE *fp;
    char ch;

    if(argc != 2)
    {
    puts("The syntax of the command is incorrect: \n*Program* *file*");
    exit(0);
    }
    if((fp = fopen(argv[1], "r")) == 0)
    {
    puts("Error opening file");
    exit(1);
    }

    while((ch = fgetc(fp))!= EOF)
    {
      switch (ch)
        {
            case ' ': putchar(ch);
                      usleep(1000000/3);
                      break;

            case '\n':putchar(ch);
                      usleep(1000000/2);
                      break;

            default: putchar(ch);
                     usleep(1000000/4);
        }

    }

    fclose(fp);
    return 0;
}

I've read about the sleep function behavior and haven't found anything relevant.

What am I doing wrong?

0 Answers0