简体   繁体   English

Linux 终端中 C 中用户输入的忘记代码

[英]Forgotten code for user input in C in Linux Terminal

I've written the code, however I cannot remember how to get the user's input.我已经编写了代码,但是我不记得如何获取用户的输入。

The command to run the code is ./rle "HHEELLLLO W" but in my code, I don't know how to get the code to read the "HHEELLLLO W" .运行代码的命令是./rle "HHEELLLLO W"但在我的代码中,我不知道如何让代码读取"HHEELLLLO W"

If I have a regular printf function, the code will work and will print H1E1L3O 0W0 but I want it so any user's input can be calculated.如果我有一个常规的printf function,则该代码将起作用并将打印H1E1L3O 0W0但我想要它,以便可以计算任何用户的输入。

I tried using atoi but I think I've done it wrong and I don't know how to fix it.我尝试使用atoi ,但我认为我做错了,我不知道如何解决它。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RLEN 50

char* encode(char* src)
{
    int rLen;
    char count[MAX_RLEN];
    int len = strlen(src);

    char* dest = (char*)malloc(sizeof(char) * (len * 2 + 1));

    int i, j = 0, k;


    for (i = 0; i < len; i++) {


        dest[j++] = src[i];


        rLen = 1;
        while (i + 1 < len && src[i] == src[i + 1]) {
            rLen++;
            i++;
        }


        sprintf(count, "%d", rLen);


        for (k = 0; *(count + k); k++, j++) {
            dest[j] = count[k];
        }
    }


    dest[j] = '\0';
    return dest;
}


int main(int argc, char ** argv)
{
    char str[] = atoi(argv[1]);
    char* res = encode(str);
    printf("%s", res);
    getchar();
}

When I compile it, I get this error:当我编译它时,我得到这个错误:

rle.c: In function ‘main’:
rle.c:47:18: error: invalid initializer
     char str[] = atoi(argv[1]);
                  ^~~~
rle.c:45:14: error: unused parameter ‘argc’ [-Werror=unused-parameter]
 int main(int argc, char ** argv)
              ^~~~
cc1: all warnings being treated as errors

atoi converts strings (of digits) to integers, which is nothing like what you to do here. atoi将字符串(数字)转换为整数,这与您在这里所做的完全不同。

I would recommend doing this one of two ways:我建议使用以下两种方法之一:

(1) Use the command-line argument directly: (1) 直接使用命令行参数:

int main(int argc, char ** argv)
{
    char* str = argv[1];
    char* res = encode(str);
    printf("%s\n", res);
}

This works because argv[1] is already a string, as you want.这是有效的,因为argv[1]已经是一个字符串,如你所愿。 (In fact in this case you don't even need str ; you could just do char* res = encode(argv[1]); .) (事实上在这种情况下你甚至不需要str ;你可以做char* res = encode(argv[1]); 。)

In this case you will have the issue that the shell will break the command line up into words as argv[1] , argv[2] , etc., so argv[1] will contain just the first word.在这种情况下,您将遇到问题 shell 会将命令行分解为argv[1]argv[2]等单词,因此argv[1]将只包含第一个单词。 You can either use quotes on the command line to force everything into argv[1] , or use the next technique.您可以在命令行上使用引号将所有内容强制输入argv[1] ,或者使用下一种技术。

(2) Read a line from the user: (2) 从用户那里读取一行:

int main(int argc, char ** argv)
{
    char str[100];
    printf("type a string:\n");
    fgets(str, sizeof(str), stdin);
    char* res = encode(str);
    printf("%s\n", res);
}

In both cases there's also some additional error checking you theoretically ought to do.在这两种情况下,还有一些你理论上应该做的额外错误检查。

In the first case you're assuming the user actually gave you a command-line argument.在第一种情况下,您假设用户实际上给了您一个命令行参数。 If the user runs your program without providing an argument, argv[1] will be a null pointer, and your code will probably crash.如果用户在不提供参数的情况下运行您的程序,则argv[1]将是 null 指针,您的代码可能会崩溃。 To prevent that, you could add the test为了防止这种情况,您可以添加测试

if(argc <= 1) {
    printf("You didn't type anything!\n");
    exit(1);
}

At the same time you could double-check that there aren't any extra arguments:同时,您可以仔细检查是否没有任何额外的 arguments:

if(argc > 2) {
    printf("(warning: extra argument(s) ignored)\n");
}

In the second case, where you prompt the user, there's still the chance that they won't type anything, so you should check the return value from fgets :在第二种情况下,您提示用户,他们仍然有可能不会输入任何内容,因此您应该检查fgets的返回值:

if(fgets(str, sizeof(str), stdin) == NULL) {
    printf("You didn't type anything!\n");
    exit(1);
}

As you'll notice if you try this, there's also the issue that fgets leaves the \n in the buffer, which may not be what you want.如果您尝试这样做,您会注意到,还有一个问题是fgets\n留在缓冲区中,这可能不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM