0

I am using the following code to open a file in write mode:

FILE *pf = fopen("c:\\test.txt", "w");
if (NULL == pf)
{
    printf("File pointer is NULL");
}

Sometime the file is created, and I get file pointer properly, but sometime I get the log "File pointer is NULL" even if the file is created on the C drive. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

0

http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx:

A null pointer value indicates an error.

In modern windows creating file on the root of drive C requires administrative privileges.

Can you reproduce the same behaviour in safe location, for example in temp directory returned by GetTempPath function?

If an error occurs, the global variable errno is set and may be used to get specific error information.

Check the 'errno' value. It should contain something like EACCES or EPERM.

You say:

...even if file is created in c drive.

I would guess that this file remains from one of previous successful runs under admin. Check the creation timestamp.

fukanchik
  • 2,811
  • 24
  • 29
  • Wow. I appreciate your sense of humor of pointing to the POSIX function manual on MSDN. – Andrejs Cainikovs Feb 12 '14 at 11:45
  • 1
    Andrejs, original question is Windows-specific. – fukanchik Feb 12 '14 at 11:55
  • Can't see anything Windows specific here so far. – Andrejs Cainikovs Feb 12 '14 at 13:23
  • I'm getting errno as '0' but even after that the file pointer is NULL & I verified the timestamp of the file, it is getting created after each run. still facing the same issue & FYI this problem is coming on windows system – user3245069 Feb 12 '14 at 13:36
  • Hmm, on my machine (Win7) errno is EACCES when i try creating file in c:\\: FILE *pf = fopen("c:\\test.txt","w"); if(NULL == pf) { printf("File pointer is NULL %d %s\n", errno, errno==EACCES?"EACCES":""); } output: File pointer is NULL 13 EACCES – fukanchik Feb 12 '14 at 14:31