0xC0000005: 读取位置 0x00000014 时发生访问冲突

martenyong 2010-01-09 12:46:08
这段代码运行的时候会出现这样的问题。是一个按钮响应程序,功能是生成指定的txt文件,并写入一些内容

void CExcelToTxt::OnBnClickedComboAreacode()
{
// TODO: 在此添加控件通知处理程序代码

CString str_excel_address = _T("\\data\\1\\1001\\Excel");
CString str_excel_name = _T("\\S0.xls");
CString str_txt_address =_T( "\\data\\1\\1001\\set_lab");
CString str_txt_name = _T("\\s0\\we.txt");
CString str;

CString sFile_txt = str_txt_address + str_txt_name;
CStdioFile file;
file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);

str=_T("define week-plan(7 records)\n");
file.WriteString(str);

file.Close();
}
...全文
681 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
martenyong 2010-01-09
  • 打赏
  • 举报
回复
绝对路径和相对路径都试过,
都是一样的问题,有一次创建了文件夹,但是没有创建txt文件,
其他的时候都无法创建文件夹
Ryanwen 2010-01-09
  • 打赏
  • 举报
回复
你用的都是相对路径 Open函数是否成功打开文件?
gaoliang1986215 2010-01-09
  • 打赏
  • 举报
回复
file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
怎么弄不成功啊?
martenyong 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ryanwen 的回复:]
\\s0这个文件夹也需要创建    你创建了?
[/Quote]

没有,创建以后就没有问题了,
谢谢。
Ryanwen 2010-01-09
  • 打赏
  • 举报
回复
\\s0这个文件夹也需要创建 你创建了?
martenyong 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ryanwen 的回复:]
C/C++ code
CFileException e;if(!file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite,&e))
{
#ifdef _DEBUG
TRACE("%d\r\n", e.m_cause);//看看这个值是多少#endif
}

str=_T("define week-pla?-
[/Quote]

e.m_cause的值是0
Ryanwen 2010-01-09
  • 打赏
  • 举报
回复

CFileException e;
if(!file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite, &e))
{
#ifdef _DEBUG
TRACE("%d\r\n", e.m_cause); //看看这个值是多少
#endif
}

str=_T("define week-plan(7 records)\n");
file.WriteString(str);

file.Close();


martenyong 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ryanwen 的回复:]
那你得先创建str_txt_address 文件夹,你的程序里一点检测机制都没有,不崩溃就怪了
C/C++ code
DWORD dwDirAttr;
dwDirAttr= GetFileAttributes(str_txt_address);if (dwDirAttr!= FILE_ATTRIBUTE_DIRECTORY)
{if (!CreateDirectory(str_txt_address, NULL))
{
printf(_T("创建str_txt_address文件夹失败!"), GetLastError());return;
}
}if(file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite))
{
str=_T("define week-plan(7 records)\n");
file.WriteString(str);

file.Close();
}


[/Quote]

谢谢,
按照这样的方法处理,文件夹可以建立,
但是txt文件依然没有存在。
Ryanwen 2010-01-09
  • 打赏
  • 举报
回复
那你得先创建str_txt_address 文件夹,你的程序里一点检测机制都没有,不崩溃就怪了

DWORD dwDirAttr;
dwDirAttr = GetFileAttributes(str_txt_address);
if (dwDirAttr != FILE_ATTRIBUTE_DIRECTORY)
{
if (!CreateDirectory(str_txt_address, NULL))
{
printf(_T("创建str_txt_address文件夹失败!"), GetLastError());
return;
}


if(file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite))
{
str=_T("define week-plan(7 records)\n");
file.WriteString(str);

file.Close();
}


martenyong 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sandyandy 的回复:]
file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);   

这个成功了么?

[/Quote]

没有。
sandyandy 2010-01-09
  • 打赏
  • 举报
回复
file.Open(sFile_txt,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);

这个成功了么?
martenyong 2010-01-09
  • 打赏
  • 举报
回复
CString str_excel_address = _T("\\data\\1\\1001\\Excel");
CString str_excel_name = _T("\\S0.xls");
以上两句中的文件夹是存在的,读取就可以了

下面两句中set_lab和s0文件夹需要创建
CString str_txt_address =_T( "\\data\\1\\1001\\set_lab");
CString str_txt_name = _T("\\s0\\we.txt");

Ryanwen 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 martenyong 的回复:]
绝对路径和相对路径都试过,
都是一样的问题,有一次创建了文件夹,但是没有创建txt文件,
其他的时候都无法创建文件夹
[/Quote]

你的意思你这些个文件夹都还没创建的?

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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