0

I'm writing this mex file:

for (k=0; k<MaxIter; k++){
    f=fopen("results.txt", "w");
    val=getValue();
    fprintf(f, "%d ", val);
    fclose(f);
    /* do something with file information */
}

Now for (MaxIter/2) iterations the file is created and written correctly but at (MaxIter/2)+1 iteration fopen() return NULL, why? maybe the file remains open?

Joseph86
  • 43
  • 1
  • 5
  • Do you mean `MaxIter/2`? And have you tried checking the return values of `fopen` and `fclose`? – Praetorian Jul 17 '14 at 05:07
  • Thanks for your response, I will check the return values as soon as possible! – Joseph86 Jul 17 '14 at 06:55
  • 2
    If you haven't checked the return values already, how do you know that they are null? Are you intentionally trying to overwrite the value in results.txt every iteration of the loop? If so, rather than opening and closing the file repeatedly, you could use `fseek (f, 0, SEEK_SET );` to rewind to the beginning of the file in the loop before you write to it. – Tom Fenech Jul 17 '14 at 07:50
  • Thanks Tom, however I have checked only the f pointer with this code: "if (!f) return error" but I don't have checked th errno variable and the fclose() return value. In the loop, after fclose(), there is a block of code that use the file information for other purposes, I needed to open-write and close file for each iteration. Sorry for my inaccuracy. Thanks a lot! – Joseph86 Jul 17 '14 at 11:29

0 Answers0