繁体   English   中英

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

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

所以下面的程序不知道有多少文件会收到,这就是为什么我使用指针向量来存储指向所有文件的指针。 无论如何,用户必须从 stdin 输入一个字符串向量,并在输入/exit时停止读取,但在此之前还必须执行命令./program file1 file2... fileN fileN之后,程序将接下来的字符串作为命令行 arguments,这不是我想要的。

第一的:

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

和标准输入:

sort
alg
di
pi
food
/exit

看一看:

#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;
}

当我按下运行按钮时,它会出现一些奇怪的错误,例如:

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

或者

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

(见代码)。 我将 malloc 更改为char text[300][300]但我仍然无法从标准输入读取文本。 它应该打印字符串向量,但如果我通过命令行 arguments 它不会执行任何操作,如果我按运行它只会给出上面的错误。

该程序旨在简单地读取文件名作为命令行 arguments(arguments 的最小数量为 1,最大数量为 9),然后是来自 stdin 的字符串向量,并在 stdout 中打印字符串向量。

(稍后将需要作为命令行 arguments 传递的文件,以使用字符串向量中的字符串搜索匹配的字符串。我没有为此编写代码,因为未正确读取字符串向量)

你能帮我同时读取命令行 arguments 和字符串向量吗?

如果您将源代码编译为c++ ,将出现错误消息invalid conversion from 'void*' to 'char**' 将结果转换为text = (char **)malloc(.. )以避免它。 如果您将代码编译为c ,则不会发生此类错误。

根据您的要求,请您尝试以下操作:

#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