创建指向文件的指针数组 [英] create array of pointers to files

查看:69
本文介绍了创建指向文件的指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何在C中创建文件指针数组?
我想创建一个指向main参数的文件指针数组,例如a1.txt,a2.txt等.因此,我将运行./prog arg1.txt arg2.txt arg3.txt以使程序使用这些文件.
那么main的参数是char **argv

How would I go about making an array of file pointers in C?
I would like to create an array of file pointers to the arguments of main... like a1.txt, a2.txt, etc... So I would run ./prog arg1.txt arg2.txt arg3.txtto have the program use these files.
Then the argument for main is char **argv

我想从argv创建文件/文件指针数组.这就是我到目前为止所拥有的.

From argv, I would like to create the array of files/file pointers. This is what I have so far.

FILE *inputFiles[argc - 1];
int i;
for (i = 1; i < argc; i++)
    inputFiles[i] = fopen(argv[i], "r");

推荐答案

代码很好,但是请记住要在C99中进行编译.

The code is fine, but remember to compile in C99.

如果不使用C99,则需要在堆上创建数组,例如:

If you don't use C99, you need to create the array on heap, like:

FILE** inputFiles = malloc(sizeof(FILE*) * (argc-1));

// operations...

free(inputFiles);

这篇关于创建指向文件的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆