请看看为什么这个文件删除不了?

ilearn 2017-11-22 05:32:27
string myclino = filename.substr(0, clipos - 1);
strcpy_s(clino, myclino.size() + 1, myclino.c_str());

string xlsfilename = filename + ".xls";
string xlsfilefullpath = pathstr + xlsfilename;

ifstream xlsfile(xlsfilefullpath);
if (xlsfile.good())
{
if (remove(xlsfilefullpath.c_str()) != 0)
{
RARCloseArchive(rarFile);
RARCloseArchive(rarFile2);
const char * errcstyle = "删除相关解压缩后的EXCEL文件出错,程序终止";
strcpy_s(errmsg, strlen(errcstyle) + 1, errcstyle);
return -6;
}
}

xlsfilefullpath 的值为 D:\\cpptestnew\\unrartestapp\\Debug\\tempdata\\A012库存20171120.xls
但删除不了,不知道什么原因
...全文
213 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2017-11-28
  • 打赏
  • 举报
回复
ifstream xlsfile(xlsfilefullpath); 这样之后可能文件被打开, 需要先调用 xlsfile.Close()来关闭. 或者其它程序或者代码打开了文件, 也同样无法删除. 请使用errno 或者 GetLastError() 获取错误码, 查看失败原因就知道了.
worldy 2017-11-28
  • 打赏
  • 举报
回复
检查remove的返回值,检查你是使用什么编码方式如果是unicode你应该使用红色的函数 remove, _wremove Delete a file. int remove( const char *path ); int _wremove( const wchar_t *path ); Routine Required Header Compatibility remove <stdio.h> or <io.h> ANSI, Win 95, Win NT _wremove <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns 0 if the file is successfully deleted. Otherwise, it returns –1 and sets errno either to EACCES to indicate that the path specifies a read-only file, or to ENOENT to indicate that the filename or path was not found or that the path specifies a directory. This function fails and returns -1 if the file is open. Parameter path Path of file to be removed Remarks The remove function deletes the file specified by path. _wremove is a wide-character version of _remove; the path argument to _wremove is a wide-character string. _wremove and _remove behave identically otherwise. All handles to a file must be closed before it can be deleted. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tremove remove remove _wremove Example /* REMOVE.C: This program uses remove to delete REMOVE.OBJ. */ #include <stdio.h> void main( void ) { if( remove( "remove.obj" ) == -1 ) perror( "Could not delete 'REMOVE.OBJ'" ); else printf( "Deleted 'REMOVE.OBJ'\n" ); }
zgl7903 2017-11-28
  • 打赏
  • 举报
回复
printf("error %d\n", errno); 根据错误代码处理
战在春秋 2017-11-27
  • 打赏
  • 举报
回复
检查一下: 文件是否为只读。 文件是否处于打开状态。 文件路径名是否正确。 另外可以换一个感觉没有问题的文件试试。
孤客天涯 2017-11-26
  • 打赏
  • 举报
回复
感觉是不是文件处于打开的状态,无法删除吧?先关闭文件然后再执行删除操作
paschen 2017-11-22
  • 打赏
  • 举报
回复
根据errno变量来确定remove执行失败的原因

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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