Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接int和FILE*(C/C+;+;)_C_File Descriptor - Fatal编程技术网

连接int和FILE*(C/C+;+;)

连接int和FILE*(C/C+;+;),c,file-descriptor,C,File Descriptor,我想根据for循环的索引来命名我的文件描述符fp。比如说, char* fbad[4]= "fbad"; char* mod[3]="mod"; for (int i=0; i<10; i++) { sprintf(fbad_file, "%s%s%d", fbad,mod,i); FILE *fp = fopen(fbad_file, "w"); ////???????????? /*then do stuff here*/ fclose(fp); } char*f

我想根据for循环的索引来命名我的文件描述符fp。比如说,

char* fbad[4]= "fbad";
char* mod[3]="mod";

for (int i=0; i<10; i++) {
  sprintf(fbad_file, "%s%s%d", fbad,mod,i);
  FILE *fp = fopen(fbad_file, "w");  ////????????????
  /*then do stuff here*/
  fclose(fp);
}
char*fbad[4]=“fbad”;
char*mod[3]=“mod”;

例如,对于(inti=0;i
,我想要实现的是:对于i=6,文件*fp6。

使用数组:

FILE *fp[10];
for(int i=0; i<10; i++) {
    fp[i] = fopen(...);
}
文件*fp[10];

因为(int i=0;i
char*fbad[4]=“fbad”;
没有做你想的。
char fbad[4]=“fbad”;
也没有。char*fbad=“fbad”;char fbad[]={“fbad”};char fbad[5]={“fbad”};谢谢@cegfault。你在最后一个问题上说得很好。因此,作为后续问题,如果文件指针不在for循环中闭合,那么:int functionName(){//do definitions and initializations here file*fp[10];for是否正确(int i=0;iyes,这在技术上是正确的。当然,这取决于你在做什么。如果你一次只使用一个文件,我个人会在for循环中完成所有操作。但是,如果你同时使用多个文件(例如,复制或比较),那么我会像你刚才那样做。