Originally Posted by
mbtm0
what function i need to use to make my cmd type text like letter by letter :/
Use fputc() (or putchar()) to output a single character, fflush() to make sure it is delivered to output (instead of cached by the C library), and sleep(), usleep(), or nanosleep() to delay between outputs.
Since you didn't mention your OS, you'd better first write a helper header file, msleep.h, to handle delays in milliseconds:
Code:
#ifndef MSLEEP_H
#define MSLEEP_H
#ifdef _WIN32
/* Use Windows Sleep() function.
*/
#include <windows.h>
static void msleep(const unsigned int milliseconds)
{
return Sleep(milliseconds);
}
#else
/* Use Linux/BSD sleep() and/or usleep() function(s).
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
#include <unistd.h>
static void msleep(const unsigned int milliseconds)
{
if (milliseconds >= 1000U)
sleep(milliseconds / 1000U);
if (milliseconds % 1000U)
usleep(1000UL * (milliseconds % 1000U));
}
#endif
#endif /* MSLEEP_H */
Here is an example function that uses the above header file. It prints the given string to standard output, one character at a time, with the specified delay in milliseconds (thousands of a second) after each character:
Code:
#include <stdio.h>
#include <string.h>
#include "msleep.h"
void slowput(const char *string, const unsigned int ms)
{
/* Return immediately if NULL. */
if (!string)
return;
/* Character loop. */
while (*string != '\0') {
/* Output character. */
putchar(*string);
fflush(stdout);
/* Next character.. */
string++;
/* Wait before the next character. */
msleep(ms);
}
return;
}
int main(int argc, char *argv[])
{
unsigned int milliseconds;
char dummy;
int arg;
if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "/?")) {
fprintf(stderr, "\nUsage: %s milliseconds string(s)..\n\n", argv[0]);
return 1;
}
if (sscanf(argv[1], " %u %c", &milliseconds, &dummy) != 1) {
fprintf(stderr, "%s: Invalid number of milliseconds.\n", argv[1]);
return 1;
}
if (milliseconds < 1U) {
fprintf(stderr, "%s: Milliseconds is too small. Use at least 1.\n", argv[1]);
return 1;
}
for (arg = 2; arg < argc; arg++) {
slowput(argv[arg], milliseconds);
/* Append space; newline after last one. */
if (arg < argc - 1)
putchar(' ');
else
putchar('\n');
}
return 0;
}
I also included the main() for an example program. It takes the delay between characters (integer milliseconds) as the first command-line parameter, and outputs the other command-line parameters using the slowput() function.
Compile it using e.g.
Code:
gcc -W -Wall -O3 filename.c -o program
and run via e.g.
Code:
./program 100 'Hello, world!'