Doubt with excel files

0

I have a list of dates in a BTH.cvs excel file. I want to copy the whole file to another new.cvs, however the a.exe stops working.

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

int main(int argc,char **argv){
    FILE *in = fopen("BTH.csv", "r");
    FILE *out = fopen("novo.csv", "w");



        while(!feof(in)){
            int num;
            fscanf(in, "%d", &num);

            fprintf(out, "%d", num);

        }




    fclose(in);
    fclose(out);


    return 0;
}
    
asked by anonymous 08.03.2018 / 21:50

1 answer

1

The problem is that the csv file has more things than numbers that you will not be able to read with %d . Although I may try to change your algorithm for this case, for example with sscanf I think it's easier follow another path.

Using the same idea as this my other answer can do so:

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

int main()
{
    FILE *in = fopen("BTH.csv", "r");
    fseek (in, 0, SEEK_END); //posicionar-se no fim
    long dimensao = ftell (in); //descobrir a dimensão
    fseek (in, 0, SEEK_SET); //voltar ao inicio

    //alocar espaço para ler tudo
    char *dados = (char*) malloc(dimensao * sizeof(char)); 

    if(dados){ //se conseguiu alocar
        fread (dados, 1, dimensao, in); //ler tudo
        FILE *out = fopen("novo.csv", "w"); //abrir o destino para escrita
        fwrite(dados, dimensao, 1 , out); //escrever todo o conteudo
        fclose(out);
    }

    fclose(in);
    return 0;
}

You have several other alternatives, such as executing a file copy by the operating system with the appropriate command.

It would also be relevant to write its error messages either for when it was unable to allocate the required reading space, or for when the opening of any of the FILE* was unsuccessful.

    
08.03.2018 / 23:47