CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    116

    _access function error

    Just wonder is it possible that if the file exist, this function below will fail by returning non-zero value?
    _access( INIFilename, 00 )

    00 - check for Existence only

    I noticed that sometimes if even the file exist, the function will fail or return non-zero value.
    So trying to find out and duplicate this error. It tends to happen intermittently.
    Any idea how can I find out what causing this error?

    Code:
    char INIFilename[256]="C:\temp\test.ini";
    unsigned long Error = 0;
    if( _access( INIFilename, 00 ) != 0 )
    {
       Error= GetLastError();   
       printf ("Failed to access INI file %s (%ul)", INIFilename, Error);
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: _access function error

    Didn't the compiler give you a warning about this string?
    Code:
    "C:\temp\test.ini"
    Hopefully you see that this string is not what you think it is, due to it having escape sequences.

    The string really is this:
    Code:
    "C:<tab>emp<tab>est.ini"
    So do you have a file that has a name with embedded tab characters? The obvious fix is to use "\\" and not "\" in string literals.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 21st, 2013 at 12:36 PM.

  3. #3
    Join Date
    Apr 2009
    Posts
    116

    Re: _access function error

    Sorry I forgot to put "\\" when posting this. My mistake on the post.
    Suppose to be like this:
    Code:
    char INIFilename[256]="C:\\temp\\test.ini";
    No, I do not have file with embedded tab characters.

    Anyway, thanks for your info. Will try to find out why.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: _access function error

    Quote Originally Posted by PHChang View Post
    Sorry I forgot to put "\\" when posting this. My mistake on the post.
    Instead of typing in the code, you should be copying and pasting the exact code you're running. Otherwise there could be other typos or missing code that affects the error that no one would know about.

    Second, GetLastError() is not used to get further information if _access() returns an error value. Please see the documentation:

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

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2009
    Posts
    116

    Re: _access function error

    Thanks for your advise. I will take note.

    Perhaps I will try to add code below to find out more on the error:
    _get_errno( &err );
    printf( "errno = %d\n", err );

    Code:
    char INIFilename[256]="C:\\temp\\test.ini";
    unsigned long Error = 0;
    errno_t err;
    if( _access( INIFilename, 00 ) != 0 )
    {
    	_get_errno( &err );
    	printf( "errno = %d\n", err );
    	Error= GetLastError();   
    	printf ("Failed to access INI file %s (%ul)", INIFilename, Error);
    }

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: _access function error

    _access will also fail if you are lacking the necessary rights to the volume, share or directory.

    typically speaking, if you can see the file in Windows Explorer, then _access(name, 0) will return 0, if you can't, then it will return error, even if the file is physically there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured