0

I'm trying to make a program which can work with several different files at the same time. My idea is to make an array of 20 FILE* in order to, if ever I arrive to this limit, to be able of closing one of them and open the new file requested. For this purpose I've thought in a function that selects an option and calls another that makes the job of saving (writing), closing and opening. After half week of searching through the web and making some proves, I've made two versions that seem to work: one with FILE* and another with FILE**.

My question is: if some of them has a mistake that I can't detect or a bad use of pointers when passing through these two functions.

(The code I display have the two versions of the two functions one after another.)

MyProgram() {

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/* The contents of myFile1a.dat, myFile2b.dat,... are just 5 ints. */


/* Version without pointer to pointer. */
void fReadFile(FILE* pfFile[]) {
    int i = 0, j = 0;
    int iNums[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int iNums2[15] = {5001, 5002, 5003, 5004, 5005, 2033, 2066, 2099, 2133, 2166, 3001, 3002, 3003, 3004, 3005};
    char cNameFile4d[32] = "myFile4d.dat", cNameFile5e[32] = "myFile5e.dat";   
    char cNameFile6f[32] = "myFile6f.dat";
    char cNameFile[32] = {" "};
    int iResultFclose = 0;

    for(i=0; i<5; i++) {
        fread(&iNums[i], 4, 1, pfFile[0]);
        printf("\n%d",iNums[i]);
    }
    for(i=5; i<10; i++) {
        fread(&iNums[i], 4, 1, pfFile[1]);
        printf("\n%d",iNums[i]);
    }
    for(i=10; i<15; i++) {
        fread(&iNums[i], 4, 1, pfFile[2]);
        printf("\n%d", iNums[i]);
    }

    iResultFclose = fclose(pfFile[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose(pfFile[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 1.");
    iResultFclose = fclose(pfFile[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 2.");

    strcpy(cNameFile, cNameFile4d);
    pfFile[0] = fopen(cNameFile, "wb");
    for(i=0; i<5; i++)
        fwrite(&iNums2[i], 4, 1, pfFile[0]);
    strcpy(cNameFile, cNameFile5e);
    pfFile[1] = fopen(cNameFile, "wb");
    for(i=5; i<10; i++)
        fwrite(&iNums2[i], 4, 1, pfFile[1]);
    strcpy(cNameFile, cNameFile6f);
    pfFile[2] = fopen(cNameFile, "wb");
    for(i=10; i<15; i++)
        fwrite(&iNums2[i], 4, 1, pfFile[2]);

    iResultFclose = fclose(pfFile[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose(pfFile[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 1.");
    iResultFclose = fclose(pfFile[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 2.");

    strcpy(cNameFile, cNameFile4d);
    pfFile[0] = fopen(cNameFile, "rb");
    strcpy(cNameFile, cNameFile5e);
    pfFile[1] = fopen(cNameFile, "rb");
    strcpy(cNameFile, cNameFile6f);
    pfFile[2] = fopen(cNameFile, "rb");

    for(i=0; i<5; i++) {
        fread(&iNums[i], 4, 1, pfFile[0]);
        printf("\n%d",iNums[i]);
    }
    for(i=5; i<10; i++) {
        fread(&iNums[i], 4, 1, pfFile[1]);
        printf("\n%d",iNums[i]);
    }
    for(i=10; i<15; i++) {
        fread(&iNums[i], 4, 1, pfFile[2]);
        printf("\n%d", iNums[i]);
    }

    iResultFclose = fclose(pfFile[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose(pfFile[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose(pfFile[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
}

/* Version with pointer to pointer. */
void fReadFile(FILE** pfFile[]) {
    int i = 0, j = 0;

    int iNums[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int iNums2[15] = {5001, 5002, 5003, 5004, 5005, 2033, 2066, 2099, 2133, 2166, 3001, 3002, 3003, 3004, 3005};
    char cNameFile4d[32] = "myFile4d.dat", cNameFile5e[32] = "myFile5e.dat", cNameFile6f[32] = "myFile6f.dat";
    char cNameFile[32] = {" "};
    int iResultFclose = 0;

    for(i=0; i<5; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[0]);
        printf("\n%d",iNums[i]);
    }
    for(i=5; i<10; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[1]);
        printf("\n%d",iNums[i]);
    }
    for(i=10; i<15; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[2]);
        printf("\n%d", iNums[i]);
    }

    iResultFclose = fclose((*pfFile)[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose((*pfFile)[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 1.");
        iResultFclose = fclose((*pfFile)[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 2.");

    strcpy(cNameFile, cNameFile4d);
    (*pfFile)[0] = fopen(cNameFile, "wb");
    for(i=0; i<5; i++)
        fwrite(&iNums2[i], 4, 1, (*pfFile)[0]);
    strcpy(cNameFile, cNameFile5e);
    (*pfFile)[1] = fopen(cNameFile, "wb");
    for(i=5; i<10; i++)
        fwrite(&iNums2[i], 4, 1, (*pfFile)[1]);
    strcpy(cNameFile, cNameFile6f);
    (*pfFile)[2] = fopen(cNameFile, "wb");
    for(i=10; i<15; i++)
        fwrite(&iNums2[i], 4, 1, (*pfFile)[2]);

    iResultFclose = fclose((*pfFile)[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose((*pfFile)[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 1.");
    iResultFclose = fclose((*pfFile)[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 2.");

    strcpy(cNameFile, cNameFile4d);
    (*pfFile)[0] = fopen(cNameFile, "rb");
    strcpy(cNameFile, cNameFile5e);
    (*pfFile)[1] = fopen(cNameFile, "rb");
    strcpy(cNameFile, cNameFile6f);
    (*pfFile)[2] = fopen(cNameFile, "rb");

    for(i=0; i<5; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[0]);
        printf("\n%d",iNums[i]);
    }
    for(i=5; i<10; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[1]);
        printf("\n%d",iNums[i]);
    }
    for(i=10; i<15; i++) {
        fread(&iNums[i], 4, 1, (*pfFile)[2]);
        printf("\n%d", iNums[i]);
    }

    iResultFclose = fclose((*pfFile)[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose((*pfFile)[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose((*pfFile)[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
}

/* Version without "&" in the caller. */
void fOptions(FILE* pfFileOpc[], int option) {
    int i = 0;
    switch (option)
    {
        case 1: {fReadFile(pfFileOpc); break;}
        case 2: {printf("\nHello!\n"); break;}
        default: break;
    }
}

/* Version with "&" in the caller. */
void fOptions(FILE* pfFileOpc[], int option) {
    int i = 0;
    switch (option)
    {
        case 1: {fReadFile(&pfFileOpc); break;}
        case 2: {printf("\nHello!\n"); break;}
        default: break;
    }
}

int main() {
    FILE *pFile[3];
    int i = 0, j = 0, option = 0, iResultFclose = 0;

    char cNameFile[32] = {" "};
    char cNameFile1a[32] = "myFile1a.dat", cNameFile2b[32] = "myFile2b.dat";
    char cNameFile3c[32] = "myFile3c.dat", cNameFile4d[32] = "myFile4d.dat";
    char cNameFile5e[32] = "myFile5e.dat",  cNameFile6f[32] = "myFile6f.dat";

    int iNums[15] = {2001, 2002, 2003, 2004, 2005, 2033, 2066, 2099,  2133, 2166, 3001, 3002, 3003, 3004, 3005};
    int iNums2[15] = {5001, 5002, 5003, 5004, 5005, 2033, 2066, 2099,  2133, 2166, 3001, 3002, 3003, 3004, 3005};

    strcpy(cNameFile, cNameFile1a);
    pFile[0] = fopen(cNameFile, "rb");
    if (!pFile[0])
        puts("Error opening the file1a");
    strcpy(cNameFile, cNameFile2b);
    pFile[1] = fopen(cNameFile, "rb");
    if (!pFile[1])
        puts("Error opening the file2b");
    strcpy(cNameFile, cNameFile3c);
    pFile[2] = fopen(cNameFile, "rb");
    if (!pFile[2])
        puts("Error opening the file3c");

    printf("\nWrite an option: \n");
    scanf("%d", &option);
    fOptions(pFile, option);

    puts("\n\nAnd we foooooolow....... \n\n");

    strcpy(cNameFile, cNameFile1a);
    pFile[0] = fopen(cNameFile, "rb");
    if (!pFile[0])
        puts("Error opening the file1a");
    strcpy(cNameFile, cNameFile2b);
    pFile[1] = fopen(cNameFile, "rb");
    if (!pFile[1])
        puts("Error opening the file2b");
    strcpy(cNameFile, cNameFile3c);
    pFile[2] = fopen(cNameFile, "rb");
    if (!pFile[2])
        puts("Error opening the file3c");

    for(i=0; i<5; i++) {
        fread(&iNums[i], 4, 1, pFile[0]);
        printf("\n%d",iNums[i]);
    }
    for(i=5; i<10; i++) {
        fread(&iNums[i], 4, 1, pFile[1]);
        printf("\n%d",iNums[i]);
    }
    for(i=10; i<15; i++) {
        fread(&iNums[i], 4, 1, pFile[2]);
        printf("\n%d", iNums[i]);
    }

    iResultFclose = fclose(pFile[0]);
    if (iResultFclose != 0)
        puts("Error closing the file 0.");
    iResultFclose = fclose(pFile[1]);
    if (iResultFclose != 0)
        puts("Error closing the file 1.");
    iResultFclose = fclose(pFile[2]);
    if (iResultFclose != 0)
        puts("Error closing the file 2.");

    return 0;
}

}

Many thanks in advance.

7
  • Compile and debug to see any error . Commented Jul 22, 2015 at 17:53
  • Are you asking for a review of the code to help you diagnose potential problems? Commented Jul 22, 2015 at 17:59
  • I can't imagine why you would use a (FILE **)[] for this. A (FILE *)[] should be perfectly adequate. As for examining your code for problems, you've posted in the wrong place. You want codereview.stackexchange.com. Commented Jul 22, 2015 at 17:59
  • I haven't read the code, but notice that FILE* pfFile[] is equivalent to FILE **pfFile, which is basically a pointer to a pointer to a FILE (a 2D FILE array). And FILE** pfFile[] is a 3D array. Commented Jul 22, 2015 at 18:02
  • Basically an array of FILE pointers should be all you need. Commented Jul 22, 2015 at 18:03

1 Answer 1

2

A single file handle is a pointer to FILE, i.e., FILE *. Since you want an array of these FILE * elements, the pointer to the array is FILE **arr, which is equivalent to FILE *arr[] when used as the type of a parameter to a function.

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

1 Comment

It is equivalent when array-to-pointer conversions take place, otherwise they're two different types.

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.