如何访问超长文件

现在,又回归到代码编写了。

其实,安安静静地享受编码的过程,是一个很美好的旅程,从此以后,就尽情享受这一过程吧。

现在,就遇到了在Windows上如何访问和操作超长文件(长度超过256)的问题。此前一直使用open、read、write等Windows原始开发接口,为照顾和Linux、Unix等接口类型的兼容性,没有使用Win32的SDK接口,这下搞不定了。

经过本次调研,发现访问超长文件名的方法,基本上有两种

一、将文件名缩短成8.3格式,继续使用原始开发接口来操作

The GetShortPathName function retrieves the short path form of a specified input path.

DWORD GetShortPathName(
  LPCTSTR lpszLongPath,  // null-terminated path string
  LPTSTR lpszShortPath,  // short form buffer
  DWORD cchBuffer        // size of short form buffer
);

Parameters

lpszLongPath
[in] Pointer to a null-terminated path string. The function retrieves the short form of this path.

Windows NT/2000/XP: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this
limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File
Name Conventions.

Windows 95/98/Me: This string must not exceed MAX_PATH characters.

lpszShortPath
[out] Pointer to a buffer to receive the null-terminated short form of the path specified by lpszLongPath.
cchBuffer
[in] Specifies the size, in TCHARs, of the buffer pointed to by lpszShortPath.

Return Values

If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpszShortPath,
not including the terminating null character.

If the lpszShortPath buffer is too small to contain the path, the return value is the size of the buffer, in TCHARs,
required to hold the path. Therefore, if the return value is greater than cchBuffer, call the function again with a buffer that is large enough to hold the path.

If the function fails for any other reason, the return value is zero. To get extended error information, call GetLastError.

从lpszLongPath参数的说明可以看出,GetShortPathNameW函数,可以对目录或文件名进行缩短,但有个前提是目录或文件名必须存在。即

1)对于已经存在的目录或文件,适用;

2)对于不存在的目录或文件(新建目录或文件时),不适用;

二、借用Win32接口来实现对超长文件名的文件或目录的操作

其中,涉及到如下的函数

Function Description
AreFileApisANSI Determines whether the file I/O functions are using the ANSI or OEM character set code page.
CancelIo Cancels all pending I/O operations that were issued by the calling thread for the specified file handle.
CloseHandle Closes an open object handle.
CopyFile Copies an existing file to a new file.
CopyFileEx Copies an existing file to a new file.
CopyProgressRoutine An application-defined callback function used with CopyFileEx and MoveFileWithProgress.
CreateDirectory Creates a new directory.
CreateDirectoryEx Creates a new directory with the attributes of a specified template directory.
CreateFile Creates or opens a file object.
CreateIoCompletionPort Creates and I/O completion port or associates an instance of an opened file with a newly created or an existing I/O completion port.
DefineDosDevice Defines, redefines, or deletes MS-DOS device names.
DeleteFile Deletes an existing file.
FileIOCompletionRoutine An application-defined callback function used with ReadFileEx and WriteFileEx.
FindClose Closes the specified search handle.
FindCloseChangeNotification Stops change notification handle monitoring.
FindFirstChangeNotification Creates a change notification handle.
FindFirstFile Searches a directory for a file whose name matches the specified file name.
FindFirstFileEx Searches a directory for a file whose name and attributes match those specified.
FindNextChangeNotification Requests that the operating system signal a change notification handle the next time it detects an appropriate change.
FindNextFile Continues a file search.
FlushFileBuffers Clears the buffers for the specified file and causes all buffered data to be written to the file.
GetBinaryType Determines whether a file is executable.
GetCurrentDirectory Retrieves the current directory for the current process.
GetDiskFreeSpace Retrieves information about the specified disk, including the amount of free space on the disk.
GetDiskFreeSpaceEx Retrieves information about the specified disk, including the amount of free space on the disk.
GetDriveType Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
GetFileAttributes Retrieves attributes for a specified file or directory.
GetFileAttributesEx Retrieves attributes for a specified file or directory.
GetFileInformationByHandle Retrieves file information for a specified file.
GetFileSize Retrieves the size of a specified file.
GetFileSizeEx Retrieves the size of a specified file.
GetFileType Retrieves the file type of the specified file.
GetFullPathName Retrieves the full path and file name of a specified file.
GetLogicalDrives Returns a bitmask representing the currently available disk drives.
GetLogicalDriveStrings Fills a buffer with strings that specify valid drives in the system.
GetLongPathName Converts the specified path to its long form.
GetQueuedCompletionStatus Attempts to dequeue an I/O completion packet from a specified I/O completion port.
GetShortPathName Retrieves the short path form of a specified input path.
GetTempFileName Creates a name for a temporary file.
GetTempPath Retrieves the path of the directory designated for temporary files.
LockFile Locks a region in an open file.
LockFileEx Locks a region in an open file for shared or exclusive access.
MoveFile Moves an existing file or a directory.
MoveFileEx Moves an existing file or a directory.
MoveFileWithProgress Moves a file or directory.
PostQueuedCompletionStatus Posts an I/O completion packet to an I/O completion port.
QueryDosDevice Retrieves information about MS-DOS device names.
ReadDirectoryChangesW Retrieves information describing the changes occurring within a directory.
ReadFile Reads data from a file, starting at the specified position.
ReadFileEx Reads data from a file asynchronously.
ReadFileScatter Reads data from a file and stores the data into a set of buffers.
RemoveDirectory Deletes an existing empty directory.
ReplaceFile Replaces one file with another file.
SearchPath Searches for the specified file.
SetCurrentDirectory Changes the current directory for the current process.
SetEndOfFile Moves the end-of-file position for the specified file.
SetFileApisToANSI Causes the file I/O functions to use the ANSI character set code page.
SetFileApisToOEM Causes the file I/O functions to use the OEM character set code page.
SetFileAttributes Sets a file‘s attributes.
SetFilePointer Moves the file pointer of an open file.
SetFilePointerEx Moves the file pointer of an open file.
SetFileSecurity Sets the security of a file or directory object.
SetFileShortName Sets the valid data length of the specified file.
SetFileValidData Sets the valid data length of the specified file.
SetVolumeLabel Sets the label of a file system volume.
UnlockFile Unlocks a previously locked region in an open file.
UnlockFileEx Unlocks a previously locked region in an open file.
WriteFile Writes data to a file.
WriteFileEx Writes data to a file asynchronously.
WriteFileGather Gathers data from a set of buffers and writes the data to a file.

举个创建或打开文件的例子吧,

<span style="font-size:14px;">HANDLE T_FileOpen_Win32(const char *filename, int oflag, int mode)
{
	int ret = 0;
	HANDLE Handle = INVALID_HANDLE_VALUE;
	WCHAR tFileName[WTP_FULL_FILENAME_LEN+1]={0};
	char apiFileName[WTP_FULL_FILENAME_LEN+1]={0};

	strcat(apiFileName,"\\\\\?\\");
	strcat(apiFileName,filename);

	ret = MultiByteToWideChar (CP_ACP, 0, apiFileName, strlen (apiFileName) + 1, tFileName, sizeof(tFileName)/sizeof(tFileName[0])) ;
	Handle = CreateFileW(tFileName, mode,FILE_SHARE_READ,NULL,oflag,FILE_ATTRIBUTE_NORMAL,NULL);
	ret = GetLastError();

	return Handle;
}</span>

如何使用呢?

1)创建或打开文件

logFileFd = T_FileOpen_Win32(transLogFileFullName,OPEN_ALWAYS,GENERIC_WRITE|GENERIC_READ);

2)只打开文件

logFileFd = T_FileOpen_Win32(transLogFileFullName,OPEN_EXISTING,GENERIC_WRITE|GENERIC_READ);

※喜欢编程开发的朋友,可以加入到“StudyRoad“QQ群(238798556)来交流,认识新朋友,开阔视野,共同提高哦!※

如何访问超长文件

时间: 2024-10-27 11:38:40

如何访问超长文件的相关文章

jquery访问ashx文件示例

转自原文jquery访问ashx文件示例 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程.其实就是带HTML和C#的混合文件. .ashx文件适合产生供浏览器处理的.不需要回发处理的数据格式,例如用于生成动态图片.动态文本等内容.很多需要用到此种处理方式.此文档提供一个简单的调用ashx文件的Demo,并贴出关键文件的源码. 以下为Demo中Login.ashx文

超过N天没有访问的文件。

编写一个脚本,清除/aspera/home 路径下超过N天没有访问的文件.清除的文件需要记录,并且带有文件最后的访问日期. 1.find /tmp/back/ -type f -atime +20 -exec /tmp/remov_file.bash {} \; 2.remov_file.bash #!/bin/sh echo $1 , `stat -c %x $1` >> /tmp/remove_file.$(date +%F).log rm -f $1 知识点详解: 1.find 可以跟脚

简单的手机访问电脑文件方法,方便写完页面即使在移动端测试效果,不使用模拟器就能玩了

手机想测问电脑写好的页面文件时,这时部分人人会想到,拷内存卡,拷优盘,直接拷手机里(当然了 用模拟器的话这里就例外了),.....普通人一般用这些复制来复制去的方法,作为一个程序员,不自觉的就感觉,这好麻烦啊,或者说好low的操作方法啊....作为一个懂电脑的人,当然不能靠这种方法了,至少不能用数据先了,优盘了,内存卡了这些媒介来拷贝吧,有些人就想了,那我打开笔记本的蓝牙,wifi把文件传手机上,至少看起来没用最low的方法,但是这依然不符合我们的风格啊. 那还想怎么玩呢?直接访问电脑文件!说到

jQuery访问json文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

ajax访问json文件缓存问题

ajax访问json文件,json文件改动,访问的时候也不能及时看到改动后的内容. 这是因为浏览器缓存的原因. 在这时候就需要清除浏览器的缓存或者加上一个标记,让ajax访问文件的时候知道这是一个新的文件,一般都是利用时间戳来解决. 解决办法:在访问ajax路径的后面加上一个时间戳参数. ajax({ method: 'POST', url: '../resource/jsonp/albumDetail/albumDetail_'+file+'.json?timestamp=new Date()

webpy 访问静态文件

1.在项目的根目录创建static文件夹 能够直接在网页中访问static文件夹中的文件 如果static文件夹有一个文件为favicon.ico,那么访问的地址为127.0.0.1:8080/static/favicon.ico 2.webpy中只提供static一个静态文件夹,如果我还需要一个存储上传文件的upload静态文件夹呢 只有自己建立一个视图函数来处理了 urls = ( '/' , 'index' , '/upload/(.*)', 'upload' , '/(js|css|im

Mac系统Finder访问资源库文件夹

Mac在Lion版本之后,默认隐藏了“资源库”文件夹,如果有时我们又需要访问它,该怎么办呢? 方法一 打开“Finder”,打开“前往”菜单时按住“Option”键. 方法二 我们也可设置Finder能够直接访问“资源库”文件夹,不要让其隐藏 打开“终端”,输入以下命令: chflags nohidden ~/Library/ 如果想回复原状,就在“终端”输入以下命令: chflags hidden ~/Library/ 原创文章,转载请注明: 转载自 http://www.mr-wu.cn/ 

【wuzhicms】apache 设置禁止访问某些文件或目录

[apache配置禁止访问] 1. 禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$"> Order allow,deny Deny from all </Files> 禁止访问某些指定的目录:(可以用 <DirectoryMatch>   来进行正则匹配) <Directory ~ "^/var/www/(.+/)*[0-9]{3}&quo

C#使用读写锁三句代码简单解决多线程并发写入文件时提示“文件正在由另一进程使用,因此该进程无法访问此文件”的问题

在开发程序的过程中,难免少不了写入错误日志这个关键功能.实现这个功能,可以选择使用第三方日志插件,也可以选择使用数据库,还可以自己写个简单的方法把错误信息记录到日志文件. 选择最后一种方法实现的时候,若对文件操作与线程同步不熟悉,问题就有可能出现了,因为同一个文件并不允许多个线程同时写入,否则会提示“文件正在由另一进程使用,因此该进程无法访问此文件”. 这是文件的并发写入问题,就需要用到线程同步.而微软也给进程同步提供了一些相关的类可以达到这样的目的,本文使用到的 System.Threadin