简单问题:写文件错误,该如何解决?

csdnxw 2003-09-29 12:01:54
打算把Win的错误消息打印打文本中去,但是在写文件的时候出现错误,
错误为:
Access wiolation at address 77366718 in module 'kernel32.dll'.
Write of address 00000000

参照BCB的帮助文件修改多次仍然不成功,请各位指教。


全部代码如下:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Msg;
char a[100];
HANDLE hFile = NULL;
int iLen;

hFile = CreateFile
(
"C:\\info.txt", // pointer to name of the file
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode
NULL, // share mode
NULL, // pointer to security attributes
CREATE_NEW | OPEN_ALWAYS, // how to create
FILE_ATTRIBUTE_ARCHIVE, // file attributes
NULL // handle to file with attributes to copy
);

if(INVALID_HANDLE_VALUE == hFile)
{
ShowMessage(SysErrorMessage(GetLastError()));
return;
}

for(int i = 0; i < 1000; i++)
{
Msg = SysErrorMessage(i);
Msg = "Hello";
iLen = Msg.Length();
strcpy(a, Msg.c_str());

WriteFile(hFile, a, iLen, NULL, NULL);
}

CloseHandle(hFile);
}
//---------------------------------------------------------------------------

...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdnxw 2003-10-05
  • 打赏
  • 举报
回复
谢谢各位
zihan 2003-10-03
  • 打赏
  • 举报
回复
如果要串口通信,还是要用到这些api,不过好像没有看到你与串口的通信代码.
如果不用这些,你可以用这个比较简单
TStringList
csdnxw 2003-10-03
  • 打赏
  • 举报
回复
谢谢BoyMgl,我现在刚开始学习BCB,对BCB的封装类不是很了解。
因为在开发串口程序的过程中用到了WinApi函数所以想到用CreateFile,
我回头试一下BCB的TFileStream类,只是现在不太熟悉。
BoyMgl 2003-10-02
  • 打赏
  • 举报
回复
不明白楼主如何想的,为什么还用API函数,bcb中有现成的TFileStream类和c库中的fopen,fwrite等等文件操作函数,还有c++中的文件流类!那个都比他方便!
ljianq 2003-09-29
  • 打赏
  • 举报
回复
以下是我一个写日志文件的代码,仅供参考:
void __fastcall TMain1::WriteLog(AnsiString sError)
{
TFileStream* FLogFile=NULL;
try {
AnsiString sCon,str=ExtractFilePath(Application->ExeName);
str+="UpdateServer"+Now().FormatString("yyyymmdd") +".Log";
if (FileExists(str))
FLogFile=new TFileStream(str,fmOpenWrite|fmShareDenyWrite);
else
FLogFile=new TFileStream(str,fmCreate|fmShareDenyWrite);

str="发生时间:";
str+=Now().FormatString("yyyy'年'mm'月'dd'日 ' hh:nn:ss") ;
sCon.cat_sprintf("%s%c%c内容:",str.c_str(),13,10);
sCon.cat_sprintf("%s%c%c %c%c",sError.c_str(),13,10,13,10);

FLogFile->Seek(0,soFromEnd);
FLogFile->Write(sCon.c_str(),sCon.Length());
}
catch(...) { ;}
try {
delete FLogFile;
FLogFile=NULL;
}
catch(...) { ; }
}
tigerhohoo 2003-09-29
  • 打赏
  • 举报
回复
写文件的时候需要一个返回参数。


void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Msg;
char a[100];
HANDLE hFile = NULL;
int iLen;
DWORD iWrite;

hFile = CreateFile
(
"d:\\test.txt", // pointer to name of the file
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode
NULL, // share mode
NULL, // pointer to security attributes
CREATE_NEW | OPEN_ALWAYS, // how to create
FILE_ATTRIBUTE_ARCHIVE, // file attributes
NULL // handle to file with attributes to copy
);

if(INVALID_HANDLE_VALUE == hFile)
{
ShowMessage(SysErrorMessage(GetLastError()));
return;
}

for(int i = 0; i < 1000; i++)
{
Msg = SysErrorMessage(i);
Msg = "Hello";
iLen = Msg.Length();
strcpy(a, Msg.c_str());

//WriteFile(hFile, a, iLen, NULL, NULL); //ERROR
WriteFile(hFile, a, iLen, &iWrite, NULL);

}

CloseHandle(hFile);

}

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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