Multiway Mergesort-External errors in C

Asked

Viewed 206 times

0

I implemented a C code of an External Multiway Mergesort ordering that reads the values in a.txt file, passes these values to the input files and migrates from the input files to the output ones until it sorts. The passing part to the input files is ok, it works. The problem lies in the part of migrating between files, specifically with one fscanf().

I have a struct that contains an array of Files and a function just to open each of the.txt input files formed and store them in the Files array, and leave them open. In another function, I read a value in each of these files already opened (which are inside the Files array in struct), through a fscanf().

I need the files to be open because the function to read them is called several times and I need to continue from where the last read was.

Unfortunately, what happens is that the program simply hangs the moment it arrives at the 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 initiating your arrays (Detail, m is a variable, in this case the amount of files that will 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 a string array with the name of the stored files.

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 opened.

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);
    }
}
  • Yes, please add the code to the body of the question, read: http://answall.com/help/mcve

1 answer

1

You got a mistake here

        int varTemp;                     // tipo de variavel
        ar = arqmanip->arquivos[inser];
        fscanf(ar, "%ld", &varTemp);     // e especificacao de scanf
                                         // incompativeis

The variable verTemp has kind int; you cannot use the specifier "%ld" address of that variable. You must use "%d" or change the type of varTemp for long.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.