简体   繁体   English

同时读取命令行 arguments 和字符串矩阵? (在 C 中)

[英]Reading command line arguments and a string matrix at the same time? (in C)

So the following program doesn't know how many files will receive that's why I used a vector of pointers to store the pointers to all files.所以下面的程序不知道有多少文件会收到,这就是为什么我使用指针向量来存储指向所有文件的指针。 Anyway, the user must input from stdin a vector of strings and the reading stop when /exit is entered, but before that the command ./program file1 file2... fileN must be executed as well.无论如何,用户必须从 stdin 输入一个字符串向量,并在输入/exit时停止读取,但在此之前还必须执行命令./program file1 file2... fileN After fileN the program takes the next following strings as command line arguments, which is not what I intended.fileN之后,程序将接下来的字符串作为命令行 arguments,这不是我想要的。

First:第一的:

./program file1.txt file2.jpg file3.in 

and stdin:和标准输入:

sort
alg
di
pi
food
/exit

Have a look:看一看:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{   char c;
    //Allocate memory for the file array based on the number of command line arguments
    FILE **f = malloc((argc-1)*sizeof(FILE*)); //invalid conversion from ‘void*’ to ‘FILE**’ {aka //‘_IO_FILE**’} [-fpermissive]
    int i;
    for (i=1; i<argc; i++) {
      printf("%s\n", argv[i]);
      f[i - 1] = fopen(argv[i],"r");
    }
    while((c= getchar()) != '\n' && c != EOF); //invalid conversion from ‘void*’ to ‘char**’  //[-fpermissive]
    char **text=malloc(300*sizeof(char*));
    for (i=0; i<=30; i++) //invalid conversion from ‘void*’ to ‘char**’ [-fpermissive]
      text[i]=malloc(300*sizeof(char));
   
    char s[30];
    int N=0;
    int ok=0;
    while (ok==0)
    {
       fgets(s,30,stdin);
       strcpy(text[N], s);
       if (strncmp(s,"/exit",5)==0)
         ok=1;
       N++;
    }
    for (i=0; i<N; i++)
    printf("%s\n", text[i]);

    // free memory after usage
    free(f);
    for(int i=0;i<30;i++) 
        free(text[i]);
    free(text);
    return 0;
}

When I press the run button it gives some weird errors like:当我按下运行按钮时,它会出现一些奇怪的错误,例如:

invalid conversion from ‘void*’ to ‘char**’ [-fpermissive]

or或者

invalid conversion from ‘void*’ to ‘FILE**’ {aka ‘_IO_FILE**’} [-fpermissive]

(see code). (见代码)。 I changed the malloc to char text[300][300] but I still could not read the text from stdin.我将 malloc 更改为char text[300][300]但我仍然无法从标准输入读取文本。 It should print the vector of strings but it doesn't do anything if I pass command line arguments and if I press run it just gives the errors above.它应该打印字符串向量,但如果我通过命令行 arguments 它不会执行任何操作,如果我按运行它只会给出上面的错误。

The program is meant to simply read file names as command line arguments (minimum number of arguments is 1 and maximum 9) and then a vector of strings from stdin and print the vector of strings in stdout.该程序旨在简单地读取文件名作为命令行 arguments(arguments 的最小数量为 1,最大数量为 9),然后是来自 stdin 的字符串向量,并在 stdout 中打印字符串向量。

(The files passed as command line arguments will be needed later to search matching strings with the strings from the vector of strings. I did not write code for that since the vector of strings is not read properly) (稍后将需要作为命令行 arguments 传递的文件,以使用字符串向量中的字符串搜索匹配的字符串。我没有为此编写代码,因为未正确读取字符串向量)

Can you help me read the command line arguments and the vector of strings at the same time?你能帮我同时读取命令行 arguments 和字符串向量吗?

The error message invalid conversion from 'void*' to 'char**' will appear if you are compiling the source code as c++ .如果您将源代码编译为c++ ,将出现错误消息invalid conversion from 'void*' to 'char**' Cast the result as text = (char **)malloc(.. ) to avoid it.将结果转换为text = (char **)malloc(.. )以避免它。 If you are compiling the code as c , such error will not occur.如果您将代码编译为c ,则不会发生此类错误。

Based on your requirements, would you please try the following:根据您的要求,请您尝试以下操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EXIT_CODE "/exit"

int main(int argc, char *argv[])
{
    char **text = NULL;                 // array of strings
    char line[BUFSIZ];                  // line buffer while reading from stdin
    char *p;                            // temporal pointer on the input line
    int n = 0;                          // line counter of stdin
    int i;                              // general index
    FILE **fp;                          // array of file pointers

    if (NULL == (fp = malloc((argc - 1) * sizeof(FILE *)))) {
        perror("malloc");
        return EXIT_FAILURE;
    }
    for (i = 1; i < argc; i++) {        // open files in the arguments
        printf("%s\n", argv[i]);
        if (NULL == (fp[i - 1] = fopen(argv[i], "rb"))) {
            perror(argv[i]);
            return EXIT_FAILURE;
        }
    }

    while (fgets(line, BUFSIZ, stdin)) {
        if ((p = strrchr(line, '\n'))) *p = '\0';       // remove trailing newline, if any
        if ((p = strrchr(line, '\r'))) *p = '\0';       // remove trailing cr character, if any
        if (NULL == (text = realloc(text, (n + 1) * sizeof(char **)))) {
            perror("realloc");
            return EXIT_FAILURE;
        }
        if (NULL == (text[n] = malloc(strlen(line) + 1))) {
                                                        // allocate memory for the input line
            perror("malloc");
            return EXIT_FAILURE;
        }
        strcpy(text[n], line);
        n++;                                            // increment the line counter
        if (strncmp(line, EXIT_CODE, strlen(EXIT_CODE)) == 0) break;
    }

    // show the input from stdin
    for (i = 0; i < n; i++)
        printf("%s\n", text[i]);

    /*
     * do your processings of the input files here
     */

    // free memory after usage
    for (i = 1; i < argc; i++)
        fclose(fp[i - 1]);
    free(fp);
    for (i = 0; i < n; i++)
        free(text[i]);
    free(text);

    return EXIT_SUCCESS;
}

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

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