关于windows的api OpenFile第一个参数的长度

hufeikong 2010-12-17 05:20:22
用OpenFile(fname, &os, OF_READ|OF_SHARE_DENY_NONE);打开文件

当fname短的时候,能打开

当路径长度长的时候就打不开

这是怎么回事啊?

怎么样能让他打开任意长度路径的文件?
...全文
285 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hufeikong 2010-12-17
  • 打赏
  • 举报
回复
漂亮,结贴
CandPointer 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hufeikong 的回复:]

明白了,谢谢楼上

我想办法把文件拷贝到短路径来计算

我确实是提取一个diff程序出来的,网上很多源码我都实验了,都不行,只有微软这个windiff是最快的

两个10万的代码,很快就比较出来了,微软的算法还不吃内存,其他的算法根本不行,速度慢,还吃内存厉害
[/Quote]

读完下面的一段MSDN的东西,你就不会在纠结长短文件名了

Naming a File
Although each file system can have specific rules about the formation of individual components in a directory or file name, all file systems follow the same general conventions: a base file name and an optional extension, separated by a period.

For example, the MS-DOS FAT file system supports 8 characters for the base file name and 3 characters for the extension. This is known as an 8.3 file name. The FAT file system and the NTFS file system are not limited to 8.3 file names, because they support a long file name.

Naming Conventions
The following rules enable applications to create and process valid names for files and directories regardless of the file system:


Use a period (.) to separate the base file name from the extension in a directory name or file name.
Use a backslash (\) to separate components in paths, which divides the file name from the path to it, or one directory from one another in a path. You cannot use a backslash in file or directory names. However, they can be required as part of volume names, for example, "C:\". UNC names must have the following format: \\<server>\<share>.
Use any character in the current code page for a name, including Unicode characters, except characters in the range of zero (0) through 31, or any character that the file system does not allow. A name can contain characters in the extended character set (128–255). However, it cannot contain the following reserved characters:
< > : " / \ | ? *

Use a period (.) as a directory component in a path to represent the current directory.
Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory.
Do not use the following reserved device names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed by an extension, for example, NUL.tx7.
Do not assume case sensitivity. Consider names such as OSCAR, Oscar, and oscar to be the same.
Do not end a file or directory name with a trailing space or a period. Although the underlying file system may support such names, the operating system does not. You can start a name with a period (.).
Maximum Path Length
In the Windows API, the maximum length for a path is MAX_PATH, which is defined as 260 characters. A path is structured in the following order: drive letter, colon, backslash, components separated by backslashes, and a null-terminating character, for example, the maximum path on the D drive is D:\<256 chars>NUL.

Note Functions in the Windows API convert "/" to "\" as part of converting the name to an NT style name.
The Unicode versions of several functions permit a maximum path length of approximately 32,000 characters composed of components up to 255 characters in length. To specify that kind of path, use the "\\?\" prefix.

Note The maximum path of 32,000 characters is approximate, because the "\\?\" prefix can be expanded to a longer string, and the expansion applies to the total length.

For example, "\\?\D:\<path>". To specify such a UNC path, use the "\\?\UNC\" prefix. For example, "\\?\UNC\<server>\<share>". These prefixes are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory. Also, you cannot use the "\\?\" prefix with a relative path. Relative paths are limited to MAX_PATH characters.

When using the API to create a directory, the specified path cannot be so long that you cannot not append an 8.3 file name.

The shell and the file system may have different requirements. It is possible to create a path with the API that the shell UI cannot handle.

Relative Paths
For functions that manipulate files, the file names can be relative to the current directory. A file name is relative to the current directory if it does not begin with one of the following:


A disk designator, which is either a drive letter followed by a colon, or a server name and share name (\\servername\sharename).
A directory name separator, such as a backslash, for example, \subdir).
If the file name begins with a disk designator and a backslash (\), it is a full path (for example, c:\tmp). If a file name begins with only a disk designator, it is a relative path to the current directory on the drive with the specified letter (for example, c:tmp.txt refers to a file in the current directory on the C drive).

Short and Long File Names
Typically, Windows stores the long file names on disk as special directory entries, which can be disabled for performance reasons. When you create a long file name, Windows also creates the short MS-DOS (8.3) form of the name. On many file systems, a short file name contains a tilde (~) character. However, not all file systems follow this convention. Therefore, do not make this assumption.

To get MS-DOS file names, long file names, or the full path of a file you can do the following:


To get an MS-DOS file name that has a long file name, use the GetShortPathName function.
To get the long file name that has a short name, use the GetLongPathName function.
To get the full path of a file, use the GetFullPathName function.

Windows stores the long file names on disk in Unicode, which means that the original long file name is always preserved, even if it contains extended characters, and regardless of the code page that is active during a disk read or write operation. The case of the file name is preserved, but the file system is not case-sensitive.

The valid character set for long file names is the Unicode file system character set minus the colon (':') that the NTFS file system uses to open alternate file streams, which means that you can copy files between the NTFS file system and FAT file system partitions without losing any file name information.






CandPointer 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hufeikong 的回复:]

明白了,谢谢楼上

我想办法把文件拷贝到短路径来计算

我确实是提取一个diff程序出来的,网上很多源码我都实验了,都不行,只有微软这个windiff是最快的

两个10万的代码,很快就比较出来了,微软的算法还不吃内存,其他的算法根本不行,速度慢,还吃内存厉害
[/Quote]


短文件名,你可以参考:
GetShortPathName


GetShortPathName Function

Retrieves the short path form of the specified path.


DWORD WINAPI GetShortPathName(
__in LPCTSTR lpszLongPath,
__out LPTSTR lpszShortPath,
__in DWORD cchBuffer
);

Parameters
lpszLongPath
The path string. The function retrieves the short form of this path.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

lpszShortPath
A pointer to a buffer to receive the null-terminated short form of the path that lpszLongPath specifies.

cchBuffer
The size of the buffer that lpszShortPath points to, in TCHARs.

Return Value


hufeikong 2010-12-17
  • 打赏
  • 举报
回复
明白了,谢谢楼上

我想办法把文件拷贝到短路径来计算

我确实是提取一个diff程序出来的,网上很多源码我都实验了,都不行,只有微软这个windiff是最快的

两个10万的代码,很快就比较出来了,微软的算法还不吃内存,其他的算法根本不行,速度慢,还吃内存厉害
CandPointer 2010-12-17
  • 打赏
  • 举报
回复
如果只是纯粹的比较文件,而不是编一个比较文件的程序


那么,UltraEdit 的 那个 UltraCompare 软件,用来比较文件很不错的
CandPointer 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hufeikong 的回复:]

是这样的,我是修改windiff的源代码来提取一个diff出来,一般的diff速度和效率都不行,我比较的文件有很大的,他里面用的是openFile ,我在调试的时候发现这个问题的

引用 7 楼 candpointer 的回复:
LZ,怎么会用openFile 这样的旧的函数?


OpenFile Function:
Note Only use this function wit……
[/Quote]


openFile 的限制,已经说明了。

如果,你的程序运行在16位windows,继续openFile ,但它不支持unicode,也就是不支持中文,长文件名。

如果你的程序在xp之类的32位windows,你用 CreateFileW 来替代它,dwCreationDisposition 参数用OPEN_EXISTING ——————可以实现和openFile一样的效果,但它支持长文件名,unicode等
hufeikong 2010-12-17
  • 打赏
  • 举报
回复
是这样的,我是修改windiff的源代码来提取一个diff出来,一般的diff速度和效率都不行,我比较的文件有很大的,他里面用的是openFile ,我在调试的时候发现这个问题的

[Quote=引用 7 楼 candpointer 的回复:]
LZ,怎么会用openFile 这样的旧的函数?


OpenFile Function:
Note Only use this function with 16-bit versions of Windows. For newer applications, use the CreateFile function.

Parameters
lpFileName
The name……
[/Quote]
CandPointer 2010-12-17
  • 打赏
  • 举报
回复
LZ,怎么会用openFile 这样的旧的函数?


OpenFile Function:
Note Only use this function with 16-bit versions of Windows. For newer applications, use the CreateFile function.

Parameters
lpFileName
The name of the file.

The string must consist of characters from the Windows character set. The OpenFile function does not support Unicode file names or opening named pipes.

CandPointer 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hufeikong 的回复:]

我用的目录路径有这么长

E:\\程序开发\\Relolver\\Release\\TempFile\\北向接口\\Src\\TMF_CORBA\\server\\Fhtmfserverdsw\\Corba_MySQLDBAPI\\mysql\\Mysql_snc.cpp_547_old

还有比这个还长的的
[/Quote]

Parameters
lpFileName
The name of the file.



The string must consist of characters from the Windows character set. The OpenFile function does not support Unicode file names or opening named pipes.



用 CreateFileW 来替代它,dwCreationDisposition 参数用OPEN_EXISTING

hufeikong 2010-12-17
  • 打赏
  • 举报
回复
我用的目录路径有这么长

E:\\程序开发\\Relolver\\Release\\TempFile\\北向接口\\Src\\TMF_CORBA\\server\\Fhtmfserverdsw\\Corba_MySQLDBAPI\\mysql\\Mysql_snc.cpp_547_old

还有比这个还长的的
hufeikong 2010-12-17
  • 打赏
  • 举报
回复
不是的,我用字符常量也实验过了,不行的

[Quote=引用 2 楼 q191201771 的回复:]
会不会是你fname长度不够,没存下完整的字符串呢? 可以打印出来看看
[/Quote]
hufeikong 2010-12-17
  • 打赏
  • 举报
回复
你的意思是这样吗?

用OpenFile("\"路径\"", &os, OF_READ|OF_SHARE_DENY_NONE);

看着很怪异,我一会调试下


[Quote=引用 1 楼 iq02006 的回复:]
是长路径的问题, 给路径字符串外面再加个“”就好了
[/Quote]
就想叫yoko 2010-12-17
  • 打赏
  • 举报
回复
会不会是你fname长度不够,没存下完整的字符串呢? 可以打印出来看看
iq02006 2010-12-17
  • 打赏
  • 举报
回复
是长路径的问题, 给路径字符串外面再加个“”就好了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧