0

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);
4
  • 1
    Here: void file_write(file_object, string_to_write){, is file_object supposed to be an array of FILE* or a single FILE*? Commented Oct 10, 2015 at 9:03
  • @CoolGuy preferably the single one, but then I would greatly appreciate if you could also explain how to create an array of file objects, outside of the function. Is it possible to create an array of files? Commented Oct 10, 2015 at 9:06
  • 3
    You could try to use a two dimension array or a one dimension arry to a char* to store all the file name/path and use indexes to refer to them as char *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. Commented Oct 10, 2015 at 9:17
  • void file_write(FILE *file_object, const char *string_to_write) Commented Oct 10, 2015 at 9:32

1 Answer 1

4

you declare a single FILE pointer and then,you can declare two arrays,one for file names,and one for the data to be written to them.make a loop and iterate like this :

int main(void)
{
    FILE *file_x;

    const char *dirs[] = { "./data/x.dat",
                           "./data/y.dat",
                           "./data/z.dat",
                           "./data/vx.dat",
                           "./vy.dat"
                         };

    const char *data[] = { "#X(t) Coordinates\n#Time (t)\n",
                           "#Y(t) Coordinates\n#Time (t)\n",
                           "#Z(t) Coordinates\n#Time (t)\n",
                           "#X(t) Velocities\n#Time (t)\n",
                           "#Y(t) Velocities\n#Time (t)\n",
                         };

    for( int n = 0 ; n < 5 ; n++ )
    {
        file_x = fopen(dirs[n],"w");

        if ( !file_x )
        {
            perror(dirs[n]);
            exit(EXIT_FAILURE);
        }
        fprintf(file_x , "%s" , data[n]);

        fclose(file_x);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

11 Comments

You open only one file at a time, process it and then close immediately, so you only need a single FILE *.
@machine_1 thanks for the answer, but the code as it is shown in the answer is leading to a seg fault works on it
That should be fprintf(file_x, "%s", data[n]);. Never use fprintf without a literal format string.
You should also check whether fopen succeeded: if (!file_x) { perror(dirs[n]); exit(EXIT_FAILURE); }
@hello_there_andy Change char * to const char *.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.