Suri
Suri

Reputation: 3357

OpenFile API returns error while opening file having name length above 127 characters

I am trying to open file which reside in a directory. It is always return error code 13 if length is above or equal 127.

char name[200]="E:\\suri_temp\\abc85\\tool\\src1111\\turi_temp\\abc85\\tool\\src1111\\puri_temp\\abc85\\tool\\src\\nuri_temp\\abc85\\to\\abcd.tmp\\suri1111.log";

int len = strlen(name); //len=127

HFILE handle ;
WORD temp;
OFSTRUCT ofstruct;

if( (handle = OpenFile(name, &ofstruct, OF_EXIST))  ==  HFILE_ERROR )
{
    temp = GetLastError(); // if length 127 or above(it comes here temp = 13)
}
else
_lclose(handle); // if length is below 127 it comes here

Anyone faced this problem?

Upvotes: 0

Views: 775

Answers (2)

Hans Passant
Hans Passant

Reputation: 941932

You are just finding out the hard way what's well documented in the MSDN article for OpenFile:

Note This function has limited capabilities and is not recommended. For new application development, use the CreateFile function.

The OFSTRUCT structure contains a path string member with a length that is limited to OFS_MAXPATHNAME characters, which is 128 characters. Because of this, you cannot use the OpenFile function to open a file with a path length that exceeds 128 characters. The CreateFile function does not have this path length limitation.

As indicated, use CreateFile() instead.

Upvotes: 3

terry999
terry999

Reputation: 1

Using

OpenFile(name, &ofstruct, OF_EXIST)) 

is ok in debug mode, but fails in release mode. Use

if(GetFileAttributes("filename") == -1)

Upvotes: 0

Related Questions