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 - execution gets blocked at fopen call. The patch of file is absolutely correct (I have already verified that) and there is no permission related issues. Can anybody please tell what could be the reason for this kind if behavior. Any kind of help is really appreciable. Is there anything related to gcc or glibc?
EDIT
Here is the sample code
printf("%s %d\n",__FUNCTION__,__LINE__);
if ((fp = fopen(argv[1], "r")) == NULL) {
printf("%s %d\n",__FUNCTION__,__LINE__);
return;
}
printf("%s %d\n",__FUNCTION__,__LINE__);
When I run this code, I only get the first print (before calling fopen) and after that program just halts. So fopen does not complete it's operation. The file is a simple configuration file with '.conf' extension and this file can be opened by all other means like vi, cat etc. There should not be any NFS related issue. Filesystem is ext3.
Thanks in advance, Souvik
If opened successfully, fopen () loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file. “w+” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file.
“r” – Searches file. Opens the file for reading only. If the file is opened successfully fopen () loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen () returns NULL.
The fopen () function opens the file whose name is the string pointed to by pathname and associates a stream with it. The argument mode points to a string beginning with one of the following sequences (possibly followed by additional characters, as described below): r Open text file for reading.
If a function sets errno on error, it will do so - doesn't matter what the previous value was. But fopen () is not required to set errno every time it returns NULL. Ah, thanks! I think I see what I was missing. to set errno to any value it wants (except zero). as before the call. Please correct me if that's not right. That's *almost* right.
Here's a few reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With