c - Array of FILE* through functions: FILE* or FILE**? -
i'm trying make program can work several different files @ same time. idea make array of 20 file* in order to, if ever arrive limit, able of closing 1 of them , open new file requested. purpose i've thought in function selects option , calls makes job of saving (writing), closing , opening. after half week of searching through web , making proves, i've made 2 versions seem work: 1 file* , file**.
my question is: if of them has mistake can't detect or bad use of pointers when passing through these 2 functions.
(the code display have 2 versions of 2 functions 1 after another.)
myprogram() { #include<stdio.h> #include<stdlib.h> #include<string.h> /* contents of myfile1a.dat, myfile2b.dat,... 5 ints. */ /* version without pointer pointer. */ void freadfile(file* pffile[]) { int = 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 file 0."); iresultfclose = fclose(pffile[1]); if (iresultfclose != 0) puts("error closing file 1."); iresultfclose = fclose(pffile[2]); if (iresultfclose != 0) puts("error closing 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 file 0."); iresultfclose = fclose(pffile[1]); if (iresultfclose != 0) puts("error closing file 1."); iresultfclose = fclose(pffile[2]); if (iresultfclose != 0) puts("error closing 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 file 0."); iresultfclose = fclose(pffile[1]); if (iresultfclose != 0) puts("error closing file 0."); iresultfclose = fclose(pffile[2]); if (iresultfclose != 0) puts("error closing file 0."); } /* version pointer pointer. */ void freadfile(file** pffile[]) { int = 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 file 0."); iresultfclose = fclose((*pffile)[1]); if (iresultfclose != 0) puts("error closing file 1."); iresultfclose = fclose((*pffile)[2]); if (iresultfclose != 0) puts("error closing 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 file 0."); iresultfclose = fclose((*pffile)[1]); if (iresultfclose != 0) puts("error closing file 1."); iresultfclose = fclose((*pffile)[2]); if (iresultfclose != 0) puts("error closing 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 file 0."); iresultfclose = fclose((*pffile)[1]); if (iresultfclose != 0) puts("error closing file 0."); iresultfclose = fclose((*pffile)[2]); if (iresultfclose != 0) puts("error closing file 0."); } /* version without "&" in caller. */ void foptions(file* pffileopc[], int option) { int = 0; switch (option) { case 1: {freadfile(pffileopc); break;} case 2: {printf("\nhello!\n"); break;} default: break; } } /* version "&" in caller. */ void foptions(file* pffileopc[], int option) { int = 0; switch (option) { case 1: {freadfile(&pffileopc); break;} case 2: {printf("\nhello!\n"); break;} default: break; } } int main() { file *pfile[3]; int = 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 file1a"); strcpy(cnamefile, cnamefile2b); pfile[1] = fopen(cnamefile, "rb"); if (!pfile[1]) puts("error opening file2b"); strcpy(cnamefile, cnamefile3c); pfile[2] = fopen(cnamefile, "rb"); if (!pfile[2]) puts("error opening file3c"); printf("\nwrite option: \n"); scanf("%d", &option); foptions(pfile, option); puts("\n\nand foooooolow....... \n\n"); strcpy(cnamefile, cnamefile1a); pfile[0] = fopen(cnamefile, "rb"); if (!pfile[0]) puts("error opening file1a"); strcpy(cnamefile, cnamefile2b); pfile[1] = fopen(cnamefile, "rb"); if (!pfile[1]) puts("error opening file2b"); strcpy(cnamefile, cnamefile3c); pfile[2] = fopen(cnamefile, "rb"); if (!pfile[2]) puts("error opening 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 file 0."); iresultfclose = fclose(pfile[1]); if (iresultfclose != 0) puts("error closing file 1."); iresultfclose = fclose(pfile[2]); if (iresultfclose != 0) puts("error closing file 2."); return 0; }
}
many in advance.
a single file handle pointer file
, i.e., file *
. since want array of these file *
elements, pointer array file **arr
, equivalent file *arr[]
when used type of parameter function.
Comments
Post a Comment