Discussion:
GetFileAttributes() and FindFirstFile() fails with MultiByte strings
(too old to reply)
s***@hotmail.com
2006-05-09 16:13:26 UTC
Permalink
Hello,

I have an excel file, whose file name contains japanese chars. When
this file is passed to GetFileAttributes() and FindFirstFile() APIs, it
fails. It returns 0xffffffff. It is a valid file, which I can otherwise
open in Excel properly.
The vc project is compiled with Multi byte character set. I have been
working with other files with file names having japanese chars, and
they all work fine. Only this file fails.
I noticed that this is due to one particular character. I copied this
character from the filename into notepad and saved it as unicode,
opened the same in vc++ editor with binary editor, it shows in hex as
3C 25 (<%).
I have no clue why these APIs are failing for this file?
I would appreciate your suggestions and comments.

Thanks
Kellie Fitton
2006-05-09 17:36:20 UTC
Permalink
Hi,

You should use the following APIs when appropriate and check
to see why the function call had failed, simply by checking
the returned error code and respond accordingly:

GetLastError()
FormatMessage()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileattributes.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileattributesex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstfile.asp

Hope these suggestions helps,

Kellie.
s***@hotmail.com
2006-05-09 18:36:06 UTC
Permalink
Thanks for your reply.

It gives "The system cannot find the file specified" error though the
file exists in the directory. Basically if I delete that one character
from the filename, it works fine.
Post by Kellie Fitton
Hi,
You should use the following APIs when appropriate and check
to see why the function call had failed, simply by checking
GetLastError()
FormatMessage()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileattributes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileattributesex.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstfile.asp
Hope these suggestions helps,
Kellie.
David Lowndes
2006-05-09 23:49:08 UTC
Permalink
Post by s***@hotmail.com
I have an excel file, whose file name contains japanese chars. When
this file is passed to GetFileAttributes() and FindFirstFile() APIs, it
fails.
The vc project is compiled with Multi byte character set.
If you create a Unicode test project, can that handle the file name
OK?
Can you pass the Unicode file name to the WideCharToMultiByte API and
see if that has a problem converting that character.

Dave
Norman Diamond
2006-05-10 04:05:44 UTC
Permalink
I have an excel file, whose file name contains japanese chars. When this
file is passed to GetFileAttributes() and FindFirstFile() APIs, it fails.
It returns 0xffffffff. It is a valid file, which I can otherwise open in
Excel properly.
The vc project is compiled with Multi byte character set.
I've reproduced a problem with long filenames but not with a particular
character as you mentioned. Also the error code for long filenames is
different from the error code you received.

I agree with David Lowndes's suggestion: Use Unicode, at least for
FindFirstFile and its relatives.
I noticed that this is due to one particular character. I copied this
character from the filename into notepad and saved it as unicode, opened
the same in vc++ editor with binary editor, it shows in hex as 3C 25 (<%).
Then you didn't convert it correctly. 3C 25 is two separate single-byte
characters in Shift-JIS and in Unicode. Your message presented these two
characters correctly inside your parentheses.
s***@hotmail.com
2006-05-11 20:06:40 UTC
Permalink
Thanks a lot for your reply.

I created a test project in unicode. But it fails there too!
When I enumerate the files in the folder with FindFirstFile() and
FindNextFile(), it reads the file name correctly.
But when I call FindFirstFile("filename") with that file name, it
returns 0xffffffff.
Post by Norman Diamond
I have an excel file, whose file name contains japanese chars. When this
file is passed to GetFileAttributes() and FindFirstFile() APIs, it fails.
It returns 0xffffffff. It is a valid file, which I can otherwise open in
Excel properly.
The vc project is compiled with Multi byte character set.
I've reproduced a problem with long filenames but not with a particular
character as you mentioned. Also the error code for long filenames is
different from the error code you received.
I agree with David Lowndes's suggestion: Use Unicode, at least for
FindFirstFile and its relatives.
I noticed that this is due to one particular character. I copied this
character from the filename into notepad and saved it as unicode, opened
the same in vc++ editor with binary editor, it shows in hex as 3C 25 (<%).
Then you didn't convert it correctly. 3C 25 is two separate single-byte
characters in Shift-JIS and in Unicode. Your message presented these two
characters correctly inside your parentheses.
David Lowndes
2006-05-11 22:56:51 UTC
Permalink
Post by s***@hotmail.com
I created a test project in unicode. But it fails there too!
When I enumerate the files in the folder with FindFirstFile() and
FindNextFile(), it reads the file name correctly.
But when I call FindFirstFile("filename") with that file name, it
returns 0xffffffff.
In the latter case, is your string a hard-coded constant (like you've
shown there), or obtained from the enumeration? In other words, is the
string presented to FindFirstFile really what you think it is?

A short (but real) example may clarify things.

Dave
s***@hotmail.com
2006-05-12 17:09:44 UTC
Permalink
Thankyou David.

I must have missed something. It is working now with Unicode char set,
but I cannot get it to work with MultiByte char set.

I tried to convert the file name using WideCharToMultiByte(), but it
does not seem to work.

And I am working with project compiled with Multi Byte Character set
project.

Any suggestions?
Post by David Lowndes
Post by s***@hotmail.com
I created a test project in unicode. But it fails there too!
When I enumerate the files in the folder with FindFirstFile() and
FindNextFile(), it reads the file name correctly.
But when I call FindFirstFile("filename") with that file name, it
returns 0xffffffff.
In the latter case, is your string a hard-coded constant (like you've
shown there), or obtained from the enumeration? In other words, is the
string presented to FindFirstFile really what you think it is?
A short (but real) example may clarify things.
Dave
David Lowndes
2006-05-12 20:01:58 UTC
Permalink
Post by s***@hotmail.com
I must have missed something. It is working now with Unicode char set,
but I cannot get it to work with MultiByte char set.
I tried to convert the file name using WideCharToMultiByte(), but it
does not seem to work.
Can you be more specific about why it doesn't work?

As always with WCTMB it will only convert characters that are
representable in the code page, so I'd assume the likelyhood is that
some character is not representable in the code page you're using.

Dave
s***@hotmail.com
2006-05-16 22:04:51 UTC
Permalink
As you have suggested, it looks like it fails to convert some
characters by WCTMB().
However, I got it working by using WCHAR version of APIs like
FindFirstFileW(). Now it works..Thanks again..
Post by David Lowndes
Post by s***@hotmail.com
I must have missed something. It is working now with Unicode char set,
but I cannot get it to work with MultiByte char set.
I tried to convert the file name using WideCharToMultiByte(), but it
does not seem to work.
Can you be more specific about why it doesn't work?
As always with WCTMB it will only convert characters that are
representable in the code page, so I'd assume the likelyhood is that
some character is not representable in the code page you're using.
Dave
Loading...