0

我试图找到一种使用方法, FILE * fopen ( const char * filename, const char * mode );但间接传递文件名。我还想知道如何间接调用名称直接取自 argv[] 的函数。我不想将字符串存储在缓冲区字符数组中。例如:

 int main (int argc,char *argv[])
    {
      FILE *src;
      ...
      src = fopen("argv[1]", "r");   //1st:how to insert the name of the argv[1] for example?
      ...
     function_call(argc,argv);    //2nd:and also how to call a function using directly argc argv
     ...
     }
    void create_files(char name_file1[],char name_file2[])
    {...}

我是否必须存储长度和字符串才能调用函数?(关于第二个问题):)

4

2 回答 2

4

你可以简单地使用argv[1],它是一个char *

if (argc < 2)
    /* Error. */

src = fopen(argv[1], "r");

也一样create_files

create_files(argv[1], argv[2]);
于 2012-01-15T14:22:27.990 回答
1

fopen接受一个指向字符数组的指针。argv是指向字符数组的指针数组的指针。

fopen(argv[1], "r")

会将 argv 数组第二个位置的指针传递给fopen.

If you want to pass argc and argv around, just pass them as they are. Their types do not change.

于 2012-01-15T14:25:08.240 回答