fopen does not return

后端 未结 4 889
感情败类
感情败类 2021-02-15 02:49

I used \'fopen\' in a C program to open a file in readonly mode (r). But in my case I observed that fopen call does not return. It does not return NULL or valid pointer - execut

相关标签:
4条回答
  • 2021-02-15 02:59

    Is it possible that you've redefined a symbol in the reserved namespace: either something beginning with two underscores, an underscore and a capital letter, or any of the standard C library functions? If so, that results in undefined behavior, and it's possible that fopen somehow ends up calling part of your code instead of the correct code in the standard library.

    This question has a major "missing information" smell to it. I seriously doubt the code snippet in the question has the behavior OP has described when it appears by itself in main, and I wonder if OP hasn't done some bogus stuff he's not telling us about...

    0 讨论(0)
  • 2021-02-15 03:08

    So what? fopen is allowed to block until the file has been opened, or until it has been determined that access is denied. If you have a slow storage device, it is absolutely correct to wait until that becomes available. But that is an operating system issue then, not C's.

    0 讨论(0)
  • 2021-02-15 03:10

    Here's a few reasons:

    • You've corrupted memory somewhere, and all bets are off as to what's happening (run your program through valgrind)
    • You're calling this code inside a signal handler, fopen() is not signal async safe, so really anything could happen (a deadlock due to the FILE* internal mutex is common though)
    • The file is a fifo , in which cases opening the file will block until someone opens the file at the other end(read/writing)
    • The file is on a stale NFS mount.
    • The file is a character/block special file with semantics that open blocks until something interesting happens,
    0 讨论(0)
  • 2021-02-15 03:19

    I notice you don't close the file if you open it successfully.

    Is it possible you that you have run it before and killed it, and now you have a process out there which has the file open, and locked?

    If so, then maybe fopen is waiting for the lock to be released.

    0 讨论(0)
提交回复
热议问题