-1

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.

5
  • 1
    It's unclear why you need two-dimensional arrays for this. Commented Feb 29, 2020 at 22:05
  • f[0,0] is the same as f[0]. Please enable compiler warnings and listen to them. Commented Feb 29, 2020 at 22:07
  • 2
    Can you elaborate on "not working"? What exactly is the expected behavior and actual behavior? Commented Feb 29, 2020 at 22:08
  • 1
    func_1(f_1); and fprintf(f[0,0], "test\n"); and func_2(f_2); are not correct. your program will not compile Commented Feb 29, 2020 at 22:10
  • Please read this. stackoverflow.com/questions/2242901/… Commented Feb 29, 2020 at 22:11

1 Answer 1

0

Instead of multiple func functions, You just need a single function that takes a FILE* array and an index. Individual files are passed as FILE*, so an array of files is passed as FILE**.

void func(FILE** file_array, unsigned int index) {
    FILE* f = file_array[index];
    if (f)
    {
        fprintf(f, "test\n");
    }
}

Then to invoke:

FILE *files[2];
files[0] = fopen("test.txt", "w");
files[1] = fopen("test_2.txt", "w");
func(files,0);
func(files,1);
fclose(files[0]);
fclose(files[1]);

You can also invoke func when all you have is a single FILE* instance. That is:

FILE* f = fopen("test.txt", "w");
func(&f, 0);

For your 2-d array, I'm not sure what you are wanting. But this:

FILE *f_2[2][2];
fprintf(f[0][0], "test\n");

Is undefined behavior. You are invoking fprintf with an uninitialized file handle.

Sign up to request clarification or add additional context in comments.

Comments

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.