C 通过函数的文件*数组:文件*还是文件**?

C 通过函数的文件*数组:文件*还是文件**?,c,file,pointers,C,File,Pointers,我正在尝试制作一个可以同时处理多个不同文件的程序。我的想法是创建一个包含20个文件*的数组,以便在达到此限制时,能够关闭其中一个文件并打开请求的新文件。 为此,我考虑了一个函数,它选择一个选项并调用另一个选项,从而完成保存(写入)、关闭和打开的工作。 在网上搜索了半个星期并做了一些证明后,我制作了两个版本,一个是FILE*版本,另一个是FILE** 我的问题是:如果其中一些函数有我无法检测到的错误,或者在传递这两个函数时指针使用不当 (我显示的代码一个接一个地包含两个函数的两个版本。) MyPr

我正在尝试制作一个可以同时处理多个不同文件的程序。我的想法是创建一个包含20个文件*的数组,以便在达到此限制时,能够关闭其中一个文件并打开请求的新文件。 为此,我考虑了一个函数,它选择一个选项并调用另一个选项,从而完成保存(写入)、关闭和打开的工作。 在网上搜索了半个星期并做了一些证明后,我制作了两个版本,一个是FILE*版本,另一个是FILE**

我的问题是:如果其中一些函数有我无法检测到的错误,或者在传递这两个函数时指针使用不当

(我显示的代码一个接一个地包含两个函数的两个版本。)

MyProgram(){
#包括
#包括
#包括
/*myFile1a.dat、myFile2b.dat……的内容只有5个整数*/
/*没有指针到指针的版本*/
作废fReadFile(文件*pfFile[]){
int i=0,j=0;
int iNums[15]={0,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;

对于(i=0;i单个文件句柄是指向
file
的指针,即
file*
。由于需要这些
file*
元素的数组,因此指向数组的指针是
file**arr
,相当于
file*arr[]
当用作函数的参数类型时。

编译并调试以查看任何错误。您是否要求查看代码以帮助诊断潜在问题?我无法想象为什么要使用
(文件**)[
。a
(文件*)[]
应该足够了。至于检查代码是否存在问题,您在错误的位置发布了。您需要。我还没有阅读代码,但请注意,
FILE*pfFile[]
相当于
FILE**pfFile
,它基本上是指向文件(2D文件数组)的指针。
FILE**pfFile[]
是一个3D数组。基本上,一个文件指针数组就是您所需要的。当发生数组到指针的转换时,它是等效的,否则它们是两种不同的类型。
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;
}