c - 如何使用已经在字符串数组中的名称进行 fopen

标签 c arguments fopen argv function-call

我正在尝试找到一种使用方法 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[])
    {...}

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

最佳答案

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

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

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

同样适用于 create_files

create_files(argv[1], argv[2]);

关于c - 如何使用已经在字符串数组中的名称进行 fopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8870328/

相关文章:

c - 多线程合并排序无法正确对数组进行排序

python - 错误 - 输入最多 1 个参数,得到 3 个

c - 写入该文件时来自单个文件的多个缓冲输入流?

linux - Linux 中的无缓冲 I/O

C - 函数的错误类型冲突

c - 如何 "undeclare"函数原型(prototype)或隐藏函数原型(prototype)或#uninclude header

c - 这是一个动态库链接器问题

C# 以相当简洁的方式向委托(delegate)传递类构造函数的参数列表

python - 阿克 : run a python script with args

php - php ubuntu Web 服务器上的 fopen 和 cURL