-2

I actually try to generate and fill random matrix and save it in plain text, txt, but have problems when I try to generate more than 1000 files than equivalent to 2000 files in working directory.

I want understand the reason and the solution for it.

I compile this code using gcc code.c and run using ./a.out; maybe change number of matrix to generate in line 25:

cant = 1000; // THIS GENERATE 1000 FILES OF CURRENT MATRIX.

code.c:


SOLUTION!!! =D

Thanks Chistopher and Vallabh

I think for me that error was an oversight on the line

close (puntero_f); close (puntero_b);

thank you very much for your explanations, I think I'll have them after even more clear concepts about files.

finally understood as post code, so I leave the solution for those who may be useful in the future

Thank you very much;)


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

//  Usando Preprocesado para manejar las matrices y tener acceso eficiente a memoria
    #define F(i,j) F[i*col +j]  
    #define B(i,j) B[i*col +j]

float randfloat(float min, float max){    
    return ((float) rand() / (float) RAND_MAX);   
}

int i, j, tstep, fil, col, cant;

int main(int argc, char *argv[]){

    char nombre_b[50] ;     //= "";
    char nombre_f[50] ;     //= "";      
    FILE *puntero_f;
    FILE *puntero_b;

    fil = 2;
    col = 2;
    cant = 1000;

    int tam = fil*col;
    float *F = calloc(tam,sizeof(float));
    float *B = calloc(tam,sizeof(float));

    //srand((unsigned)time(NULL));

    int archi = 0;
    for (tstep=0; tstep<cant; tstep++){

        sprintf(nombre_f, "Forward%d.txt", tstep);
        sprintf(nombre_b, "Backward%d.txt", tstep);
        puntero_f = fopen(nombre_f, "a+");
        puntero_b = fopen(nombre_b, "a+");

        for(i = 0; i<fil; i++){
            for(j = 0; j<col; j++){
                F(i,j) = randfloat(0.0f, 1.0f);
                B(i,j)  = randfloat(0.0f, 1.0f);

                if(tstep==archi){
                    fprintf(puntero_f,"%f ", F(i,j));
                    fprintf(puntero_b,"%f ", B(i,j));
                }
            }
            if(tstep==archi){
                fprintf(puntero_f,"\n");
                fprintf(puntero_b,"\n");
            }
        }
        archi++;
        fclose(puntero_f);
        fclose(puntero_b);
    }

    return 0;
}

at this point a question arises me, that is not generating more than 1000 files?

txt only generates up Foward999.txt? files?

William Trigos
  • 360
  • 1
  • 10
  • What does `Dato()` do? – dlf Jul 17 '14 at 17:48
  • 1
    We cannot help unless we see your actual source code. What you've posted will not compile. It's missing at least two `#include` directives as well as declarations of `Dato` and `randfloat`. If `Dato` is a function, a call cannot appear on the left side of an assigment. Copy-and-paste the actual code you compiled into the question. – Keith Thompson Jul 17 '14 at 17:50
  • 1
    2 questions: have you tried using fclose() instead of close() ? have you verified that you have enough space on your disk ? – Christophe Jul 17 '14 at 17:50
  • 1
    Also the exact text of the seg-fault usually has useful information. – AShelly Jul 17 '14 at 17:52
  • 1
    @KeithThompson A function call can appear on the left side of an assignment as long as it returns something assignable. In this case, I'd guess it returns a reference to a particular float in the input array. But if there is an error in `Dato` and it's returning a wild reference, that would explain the crash. – dlf Jul 17 '14 at 17:53
  • @dlf: C doesn't have references. – Keith Thompson Jul 17 '14 at 18:00
  • @dlf: how do you do that in C? I'm pretty sure that `Datao()` is a macro that performs a 2D index mapping into the float array specified by the first pointer argument. But that macro should be provided by the OP so there's no guesswork (and there's a good chance that's where the bug is). – Michael Burr Jul 17 '14 at 18:00
  • @KeithThompson the only way to have a function on left died of assignment in plain C is to use a macro. – Christophe Jul 17 '14 at 18:01
  • @KeithThompson Darn. The question *did* have the C++ tag on it earlier... – dlf Jul 17 '14 at 18:01
  • One more thing: Your calls to `close()` should be calls to `fclose()`. You can't legally pass a `FILE*` to the `close()` function -- which means that, even with the necessary `#include` directives, you should have gotten some warnings from your compiler. Show us your *complete* source code and any warnings your compiler gave you. Copy-and-paste both into the question; don't try to re-type or summarize. – Keith Thompson Jul 17 '14 at 18:04
  • Compile the code with `gcc -g code.c` and then run it with `gdb ./a.out` and then type 'run' at the gdb prompt to start your program. – indiv Jul 17 '14 at 18:04
  • @KeithThompson yes that's what I said above (third comment) – Christophe Jul 17 '14 at 18:12
  • @Christophe: You didn't mention compiler warnings. – Keith Thompson Jul 17 '14 at 18:13
  • @KeithThompson sorry, I was talking about root cause. The problem is that open() is not defined in stdio.h, so the warning is just that it's an external definition, which could be overseen. If unistd.h would have been included, the warning would have been more explicit. – Christophe Jul 17 '14 at 18:22
  • 1
    @Christophe: I just noticed that gcc actually doesn't warn about the `close` calls unless you ask it to (which is unfortunate). Under the rules of the "gnu89"/"gnu90" dialect it enforces by default, it assumes an implicit declaration for `close`. Compiling with `-std=c99` or `-std=gnu99` does produce a warning. But the code in the question would have gotten a number of other warnings (some because of missing code that must be in the original but the OP hasn't shown us). – Keith Thompson Jul 17 '14 at 18:33
  • Don't edit the solution into your question. The way to indicate that your problem has been solved is to accept one of the answers (click the checkmark). – Keith Thompson Jul 17 '14 at 20:42
  • Some remaining problems with your code: Your `randfloat` function ignores its `min` and `max` parameters. The `#include ` is unnecessary, since you don't use anything from that header. It's commented out, but `srand((unsigned)time(NULL));` should be just `srand(time(NULL));`; the value will be converted to the correct type implicitly. – Keith Thompson Jul 17 '14 at 20:44

2 Answers2

1

The problem is the call to close(). If you have fopen() use fclose().

open()/close() work with file descriptors which are ints,

fopen()/fclose() work with FILE*.

The problem in your code is that poitners can be converted to ints, so the code compile, but the result is a crash ! I compiled your code with fclose() and it works perfectly.

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

You shouldn't use fopen() with close() or open() with fclose()

In your code instead of close() use fclose()

fopen() returns FILE pointer and close() need file descriptor to operate on.

as indicated by @christopher close()/open() deals with file descriptor while fopen()/fclose() deals with FILE pointer.

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40