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

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

问题描述

我将如何在 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天全站免登陆