c - 如何从函数返回一个 char 值?在 Unix 中

标签 c unix

用 C 编写程序,从文件中读取行 文本,其名称将在运行时提供给用户。 程序应该选择文件的随机行并打印屏幕,给用户 X 时间来键入屏幕上看到的单词。 X 的时间取决于每个短语的长度,你可以认为每个字符都会给用户 1 秒。 如果消息正确并按时打印,则用户会收到祝贺。如果错误信息被打印出来(而且是准时的),那么用户就会被准确地告知他所犯的错误。

最后,如果在打印消息之前时间用完,则询问用户是否要继续,如果用户回答是,则对文件的新随机行重复上述序列,否则程序终止。

谁能告诉我我做错了什么;如果您编写所需的代码,我会更容易......

谢谢你

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

void catch_alarm (int sig)
{
    char ans[2];

    printf ("Operation timed out. Exiting...\n");
    printf ("thes mia akoma eukairia?[y/n]\n");
    scanf ("%s", ans);

    exit (0);
}
void exodos (int sig)
{
    printf ("termatismos ergasias... \n");
    signal (SIGQUIT, SIG_DFL);

    exit (0);
}

int main (int argc, char **argv)
{
    int i, j, x, count, gram[80];
    i = j = count = 0;

    char arxeio[25], grammi[80], buf[80][80], protash[80],  ch, ans[2];
    FILE *fp;

    printf("dwse to onoma tou arxeiou pou thes na anoixeis: \n");
    scanf("%s", arxeio);

    do
    { 
        fp = fopen( arxeio, "r");
        while ( (ch = getc(fp)) != EOF )
        { 
            buf[i][j] = ch;
            if ( buf[i][j] == '\n' )
            {
                ++i;
                gram[i] = j;
                j = 0;
            }
                ++j;
        }

        fclose(fp);

        // edw vazoume tin rand kai to apotelesma tis sto 4 parakatw
        x = rand() % i;   
        j = 0;

        while (j<=gram[x+1])
        { 
            printf("%c", buf[x][j]);
            j++;
        }


        /* elenxos entos xronou an oxi TIME OUT... */
        signal(SIGALRM, catch_alarm);
        fflush(stdout);
        alarm(gram[x+1]);
        scanf("%s",protash);

        if (ans[0] == 'n')  
            signal(SIGQUIT, exodos);

        /* elenxos or8hs eisagwghs dedomenwn*/
        j = 0;
        while ( j<=(gram[x+1]-2) )
        {  
            if ( buf[x][j+1] != protash[j] )
                printf("anti gia %c egrapses %c\n", buf[x][j+1], protash[j]);
            else    
                printf("swsto\n");

            ++j;
        }

        /* deuterh eukairia... */


    }
    while ( ans[0] == 'y' ); 

    return 0;
}

最佳答案

您在此处粘贴的示例有点难以理解,但看起来您正试图让信号处理程序返回一个字符。这是不可能的,因为信号处理程序必须为 void(尽管函数通常可以返回 char)。

解决此问题的最简单方法是使用全局(静态?)变量。

还要注意 printf 和 scanf 不是 async-safe .解决方法是在某处设置一个“标志”,然后注意它已在稍后设置。

编辑 我认为这是您要在此处实现的目标的简化示例:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <errno.h>

static int timeout = 0;

static void catch_alarm(int sig) {
   if (SIGALRM != sig || 0 != timeout)
      abort();

   timeout = 1;
}

int main() {
   char buf[80];

   signal(SIGALRM, catch_alarm); //TODO: check return!

   printf("Type some stuf:\n");
   fflush(NULL);
   alarm(5);
   int read = -1;
   while (read  < 0 && !timeout) {
      read = scanf("%80s", buf);
   }

   if (timeout) {
     printf("Time out, do something else\n");
   }

   exit(0);
}

关于c - 如何从函数返回一个 char 值?在 Unix 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4124291/

相关文章:

c - kill -9 或 ctrl+c 推迟所有已在 C 中调用的函数?

c - 我应该更正哪个指针语句以避免段错误

windows - cygwin curl、Wget、wget 和 top "command not found"

linux - zsh提示功能不起作用

c++ - 如何检查端口是否处于 TIME_WAIT 状态或已被另一个应用程序占用

c++ - 使异步套接字服务器和客户端工作(用 C 编写)

c++ - C/C++ 中的单引号、双引号和 sizeof ('a' )

c - 我不知道如何使用libcs​​v中某些函数的参数

oracle - 如何使用 REGEXP_SUBSTR 提取最后一个字符串?

c - Unix 中主机名的最大字符数是多少?