求救!!!!!!关于std::remove的一点问题

zk19870803 2015-03-19 04:15:37
在我的程序中,会创建一个临时文件,使用wb+打开的,在程序结束时,我会在fclose掉文件后,使用::remove();来删除该文件。

现在问题来了,fclose关闭文件正常,但是在调用remove时返回错误,查看错误码发现错误为permission denied。

我是win7的系统,但是在win7下使用xp的虚拟机,依然会出现这个问题。

求教各位大神,怎么样用代码的方式来解决这个问题,不要说什么用管理员方式运行。。。
...全文
126 点赞 收藏 7
写回复
7 条回复
赵4老师 2015年03月19日
MoveFileEx The MoveFileEx function renames an existing file or directory. BOOL MoveFileEx( LPCTSTR lpExistingFileName, // pointer to the name of the existing file LPCTSTR lpNewFileName, // pointer to the new name for the file DWORD dwFlags // flag that specifies how to move file ); Parameters lpExistingFileName Pointer to a null-terminated string that names an existing file or directory on the local machine. lpNewFileName Pointer to a null-terminated string that specifies the new name of lpExistingFileName on the local machine. When moving a file, the destination can be on a different file system or drive. If the destination is on another drive, you must set the MOVEFILE_COPY_ALLOWED flag in dwFlags. When moving a directory, the destination must be on the same drive. If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT, lpNewFileName can be NULL. In this case, MoveFileEx registers the lpExistingFileName file to be deleted when the system reboots. If lpExistingFileName refers to a directory, the system removes the directory at reboot only if the directory is empty. dwFlags Set of bit flags that specify how to move the file. You can specify any combination of the following values: Value Meaning MOVEFILE_COPY_ALLOWED If the file is to be moved to a different volume, the function simulates the move by using the CopyFile and DeleteFile functions. This flag cannot be used with the MOVEFILE_DELAY_UNTIL_REBOOT flag. MOVEFILE_DELAY_UNTIL_REBOOT The function does not move the file until the operating system is restarted. The system moves the file immediately after AUTOCHK is executed, but before creating any paging files. Consequently, this parameter enables the function to delete paging files from previous startups. This flag can only be used if the process is in the context of a user who belongs to the administrator group or the LocalSystem account. This flag cannot be used with the MOVEFILE_COPY_ALLOWED flag. MOVEFILE_REPLACE_EXISTING If a file of the name specified by lpNewFileName already exists, the function replaces its contents with those specified by lpExistingFileName. MOVEFILE_WRITE_THROUGH The function does not return until the file has actually been moved on the disk. Setting this flag guarantees that a move performed as a copy and delete operation is flushed to disk before the function returns. The flush occurs at the end of the copy operation. This flag has no effect if the MOVEFILE_DELAY_UNTIL_REBOOT flag is set. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks If the dwFlags parameter specifies MOVEFILE_DELAY_UNTIL_REBOOT, MoveFileEx stores the locations of the files to be renamed at reboot in the following registry value: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations The function fails if it cannot access the registry. The PendingFileRenameOperations value is of type REG_MULTI_SZ. Each rename operation stores a pair of NULL-terminated strings. The system uses these registry entries to complete the operations at reboot in the same order that they were issued. For example, the following code fragment creates registry entries that delete szDstFile and rename szSrcFile to be szDstFile at reboot: MoveFileEx(szDstFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT); MoveFileEx(szSrcFile, szDstFile, MOVEFILE_DELAY_UNTIL_REBOOT); The system stores the following entries in PendingFileRenameOperations: szDstFile\0\0 szSrcFile\0szDstFile\0\0 Because the actual move and deletion operations specified with the MOVEFILE_DELAY_UNTIL_REBOOT flag take place after the calling application has ceased running, the return value cannot reflect success or failure in moving or deleting the file. Rather, it reflects success or failure in placing the appropriate entries into the registry. The system deletes a directory tagged for deletion with the MOVEFILE_DELAY_UNTIL_REBOOT flag only if it is empty. To ensure deletion of directories, move or delete all files from the directory before attempting to delete it. Files may be in the directory at boot time, but they must be deleted or moved before the system can delete the directory. Windows 95 and Windows 98: The MoveFileEx function is not supported. To rename or delete a file at reboot, use the following procedure. To rename or delete a file on Windows 95 and Windows 98 Check for the existence of the WININIT.INI file in the Windows directory. If WININIT.INI exists, open it and add new entries to the existing [rename] section. If the file does not exist, create the file and create a [rename] section. Add lines of the following format to the [rename] section: DestinationFileName=SourceFileName Both DestinationFileName and SourceFileName must be short filenames. To delete a file, use NUL as the value for DestinationFileName. The system processes WININIT.INI during system boot. After WININIT.INI has been processed, the system names it WININIT.BAK. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Unsupported. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, CopyFile, DeleteFile,GetWindowsDirectory, WritePrivateProfileString
回复 点赞
zk19870803 2015年03月19日
求调用::remove();不失败的方法,或者其他删除文件的函数都可以啊,跪求各路大神了。。
回复 点赞
赵4老师 2015年03月19日
利用链接http://www.nirsoft.net/utils/opened_files_view.html中的工具软件可以查看指定文件目前正在被哪个进程打开,还可以强行关闭之,但后果自负。
回复 点赞
HiChengzi 2015年03月19日
有创建的权限 为啥不能删呢
回复 点赞
zk19870803 2015年03月19日
引用 3 楼 zhao4zhong1 的回复:
std::remove 和 ::remove 不是一回事! http://www.nirsoft.net/utils/opened_files_view.html
这个确实是一时手贱,链接的意思是只能用管理员方式运行,没有其他的办法了?
回复 点赞
赵4老师 2015年03月19日
std::remove 和 ::remove 不是一回事! http://www.nirsoft.net/utils/opened_files_view.html
回复 点赞
zk19870803 2015年03月19日
引用 1 楼 zhao4zhong1 的回复:
用管理员方式运行 你又能把我怎么样!
赵老师,别这样啊。。求方法。。
回复 点赞
赵4老师 2015年03月19日
用管理员方式运行 你又能把我怎么样!
回复 点赞
发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告