I need to modify a program written in C, which I have very little experience with and I would like to handle an array of files. However, I am experiencing a problem of handling with pointers when I want to write into these files...
Here's the minimal code to see what I want to do:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(FILE *f){
fprintf(f, "test\n");
}
void func_1(FILE *f){
fprintf(f[0], "test\n");
}
void func_2(FILE *f){
fprintf(f[0][0], "test\n");
}
int main() {
FILE *f = fopen("test.txt", "w");
func_1(f);
fclose(f);
FILE *f_1[2];
f_1[0] = fopen("test.txt", "w");
f_1[1] = fopen("test_2.txt", "w");
func_1(f_1);
fclose(f_1[0]);
fclose(f_1[1]);
FILE *f_2[2][2];
fprintf(f[0][0], "test\n");
f_2[0][0] = fopen("test.txt", "w");
f_2[1][1] = fopen("test_2.txt", "w");
func_2(f_2);
fclose(f_2[0][0]);
fclose(f_2[1][1]);
}
Basically, the first part with f is working but not when I am creating f_1 or f_2 which are arrays of files. How to handle them to write into some of them specifically.
Sincerely, Paul
EDIT: As noted my minimal code is not running and is confusing. It is my bad, I continue on trying other things and forgot to remove the test modifications.
f[0,0]is the same asf[0]. Please enable compiler warnings and listen to them.