开发者

fopen is not opening a file stream... how do I resolve this?

开发者 https://www.devze.com 2023-02-11 17:39 出处:网络
I am trying to write a file and append data to it. Here is a snippet of my code.thresh is an unsigned short.

I am trying to write a file and append data to it.

Here is a snippet of my code. thresh is an unsigned short.

    FILE *fp_th;
    fp_th = fopen("threshold.txt", "a");
    printf("opening file failed: %s\n", strerror(errno));
fprintf(fp_th,"%d ", thresh);
    fclose(fp_th);

Before it has worked fine, but I've changed some of my code and all of a sudden it doesn't print out anymore.

I've confirmed that fopen is开发者_运维技巧n't opening a file stream with

printf(" check fp_th = %p \n", fp_th);

It prints out check fp_th = 00000000.

EDIT: Added printf("opening file failed: %s\n", strerror(errno)); after fopen. Output says no error. Does it lie? Odd..

The odd thing is that I am writing and appending a similar file, and yet that file works fine. How do you resolve a fopen that returns a NULL? Why does it happen? Thanks!


Firstly

fprintf(fp_th,"%d ", thresh);

Will normally buffer output until you close the file, or the internal FILE* buffer is full. You might not see any output in the file immediatly

You could fflush() the FILE* to have it written out to the file when you decide.

fprintf(fp_th,"%d ", thresh);
fflush(fp_th);

Secondly

printf(" check fp_th = %d \n", fp_th);

This does not check that fp_th is invalid. It just prints the pointer value of fp_th as a signed integer, and -7323824 might be as good as a value as any, this will be even less meaningful, and probably undefined if the size of your pointers are not the same as the size of an int.

To print a pointer you should use %p

printf(" check fp_th = %p \n", fp_th);

fopen returns NULL if it fails, you should check for that to learn if opening the file failed or not.

FILE *fp_th;
fp_th = fopen("threshold.txt", "a");
if(fp_th == NULL) {
   printf("opening file failed: %s\n", strerror(errno));
   return;
}


First, always check the return value of fopen for NULL, which means failure.

To find out what went wrong during the file open, you can use perror.

  if (fp_th == NULL) {
    perror ("Error opening threshold file");
    exit(EXIT_FAILURE);
  }

If you still cannot figure out while you get permission denied or No such file or directory, you can use strace (assuming Linux) and to look for the corresponding system call.


Before it has worked fine, but I've changed some of my code and all of a sudden it doesn't print out anymore.

If your code changed the current working directory, this can cause the file to not be found, as you're not providing a full path. In this case, fp_th should be a null pointer, which is something for which you could check. Your print suggests that it's not returning a null pointer, but rather did open the file. For details, see fopen.


I had a similar problem today. My code was openning N-1 files but mysteriously crashed in the Nth file, although that file definitely existed ...

perror revealed that I actually never closed all these files -to my embarassment, I had forgotten fclose(fd)...

I didn't use perror so far, but I now realize how valuable it is. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消