简单问题:写文件错误,该如何解决?
打算把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);
}
//---------------------------------------------------------------------------