I implemented a C-code of an External Multiway MergeSort sort that reads the values into a .txt file, passes these values to the input files, and migrates from the input files to the output files, until ordered. The part of passing into the input files is okay, it works. The problem is in the part of migrating between one file to another, specifically with a fscanf()
.
I have a struct that contains an array of FILEs and a function just to open each of the input.txt files formed and store them in the array of FILEs, and leave them open. In another function, I read a value in each of these already open files (which are inside the array of FILEs in the struct), through a fscanf()
.
I need the files to be open because the function to read from them is called several times and I need to continue from where the last reading was done.
Unfortunately, what happens is that the program simply hangs when it arrives at fscanf()
.
I can send the code to test.
Respectively: a struct with the FILES array (the rest is an int array for future use). the function to open the files and leave them open and the function that reads the contents of each file already opened.
typedef struct TArquivos{
FILE **arquivos;
int *restantes;
}TArquivos;
Creating the struct and initializing its arrays (Detail, m is a variable, in this case, the amount of files to be read
TArquivos *manipula = (TArquivos*)malloc(sizeof(TArquivos));
manipula->arquivos = (FILE**)malloc(sizeof(FILE*)*m);
manipula->restantes = (int*)malloc(sizeof(int)*m);
The function to open the files and leave them open. Ars_input is an array of string with the name of the files stored.
void abrearquivos(TArquivos *manipul, char **arqs_Input, int m){
int co;
for(co = 0;co<m;co++){
manipul->arquivos[co] = fopen(arqs_Input[co], "r");
}
}
The function that reads the contents of each file already open.
void inserePrimeiros(TNo *Arv, TArquivos *arqmanip, int cont){
int inser;
FILE *ar;
for(inser = 0; inser<cont; inser++){
arqmanip->restantes[inser]--;
int varTemp;
ar = arqmanip->arquivos[inser];
fscanf(ar, "%ld", &varTemp);
TValores *valor = (TValores*)malloc(sizeof(TValores));
valor->arquivoCorresp = inser;
valor->n = varTemp;
insereArvore(valor, Arv);
}
}