简体   繁体   中英

Concatenate int and FILE* (C/C++)

I would like to name my file descriptors, fp, based on the index of the for loop. For instance,

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);
}

How does one concatenate *fp and i such shat the descriptor is unique for every file opened? For example, what I want to achieve is: for i=6, FILE *fp6.

Thanks in advance.

For example, what I want to achieve is: for i=6, FILE *fp6.

Use an array:

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

Although, if you are closing the file pointer inside of the for-loop, what is the problem with reusing fp ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM