I would like to iterate over a list/array of file objects: file_x,file_y,file_z,..., calling a function on each file object, in C.
Question
How can I create a function that would take (i) file object, and (ii) a string as arguments to the function and then write the string to that file object. A For loop could then execute the function over a list/array of file objects.
void file_write(file_object, string_to_write){
fprintf(file_object, "%s\n", string_to_write);
}
Research
I have searched Google, watched parts of several Youtube tutorials and searched for relevant questions on SO, but the following is the best I could achieve. Any advice on how to better answer the above question, with the ideal function, would be much appreciated.
FILE *file_x, *file_y, *file_z, *file_vx, *file_vy
file_x = fopen("./data/x.dat","w");
file_y = fopen("./data/y.dat","w");
file_z = fopen("./data/z.dat","w");
file_vx = fopen("./data/vx.dat","w");
file_vy = fopen("./vy.dat","w");
fprintf(file_x, "#X(t) Coordinates\n#Time (t)\n");
fprintf(file_y, "#Y(t) Coordinates\n#Time (t)\n");
fprintf(file_z, "#Z(t) Coordinates\n#Time (t)\n");
fprintf(file_vx, "#X(t) Velocities\n#Time (t)\n");
fprintf(file_vy, "#Y(t) Velocities\n#Time (t)\n");
fclose(file_x);
fclose(file_y);
fclose(file_z);
fclose(file_vx);
fclose(file_vy);
void file_write(file_object, string_to_write){, isfile_objectsupposed to be an array ofFILE*or a singleFILE*?char*to store all the file name/path and use indexes to refer to them aschar *fileName[num of files] = {"path/name", ...};This way you can use a for loop to open them and store the opened file descriptor to another array which you can another for loop to loop through can call your function.void file_write(FILE *file_object, const char *string_to_write)