0

I have to write some results in an xls file, but I need to create a single file that contains everything, so far I create one file for each iteration, but I do not know how to make it write one result followed by another.

this is my code (corrected),

void writeXLS(int numCell,int dmax,int ite,float **x){

    char it [5];
    itoa(ite,it, 10);

    char *name = concat("resul",it);
    char *arc  = concat(nombre,".xls");

    FILE *Resul;
    Resul=fopen(arc,"w");

        for(int j=0;j<numCell;j++){
            for(int k=0;k<dmax;k++){
                fprintf(Resul,"%.3g\t",x[0][j*dmax+k]);
            }
            fprintf(Resul,"\n");
        }
    fclose(Resul);
}

I can´t open the file outside the function .. is a huge project and I open others files for write and read.

I call writeXLS() in each iteration and need to write down in XLS file.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    Can't you use a library for that? There are several that read and write excel files. https://libxlsxwriter.github.io/ – drescherjm Oct 19 '17 at 18:45
  • Offtopic: Please consider more international friendly code conventions. Few of us know spanish and proper naming greatly alliviates reviewing of code. – Gnqz Oct 19 '17 at 18:46
  • Open and close the file *outside of* the function. Open it before the first call to `escribirResultados` and close it after the last call. Pass `FILE *Resultados` as a function argument. – Weather Vane Oct 19 '17 at 18:52
  • sorry for my poor english, I tried to correct and make simple my code – Viviana Leyva Cifuentes Oct 19 '17 at 18:58
  • 1
    The edited question says you can't open the file outside of the function. So in the function, can you open it for **append** with `Resul=fopen(arc,"a");`? – Weather Vane Oct 19 '17 at 19:31
  • *is a huge project* In a "huge project", [code like this smells](https://en.wikipedia.org/wiki/Code_smell): `char *name = concat("resul",it);` With multiple calls to `concat()` and no later `free( name )`, the only conclusions possible are either `concat()` is returning a pointer to a static buffer and the multiple calls are overwriting each other's data, or you're leaking memory badly. – Andrew Henle Oct 20 '17 at 14:19
  • Possible duplicate of [Append to the end of a file in C](https://stackoverflow.com/questions/19429138/append-to-the-end-of-a-file-in-c) – This company is turning evil. Nov 06 '17 at 19:42

1 Answers1

0

I change the creation of File

FILE *Resul;
Resul=fopen(arc,"a+");

Append = a+

that's all, thanks for your colaboration.