在 Visual Studio 中写入文件时出错

Error Writing to File in Visual Studio

本文关键字:文件 出错 Visual Studio      更新时间:2023-10-16
Unhandled exception at 0x102e1cee (msvcr100d.dll) in filename.exe 0xC0000005: Access violation writing location 0x00416858 on.

调试点到行:

if (_putc_nolock(ch, f) == EOF)

代码数量

#else  /* _UNICODE */
    if (_putc_nolock(ch, f) == EOF)
#endif  /* _UNICODE */
        *pnumwritten = -1;
    else
        ++(*pnumwritten);
}

在output.c中,我相信它在Visual Studio库中链接。我没有链接它。

我的代码是:

body=""
myFile=CreateFile("Sample.txt",FILE_APPEND_DATA,FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
BufferNo=sprintf(body,"%.5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f n",a1,a2,a3,a4,a5,a6,a7,a8);
WriteFile(myFile,body,lstrlen(body),0,NULL);
CloseHandle(myFile);

我最初用以下几行写文件。我不得不写标题。

HANDLE myFile=CreateFile("Sample.txt",GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0);
char* HeadingStr="a1   a2   a3   a4   a5   a6   a7   a8 n";
WriteFile(myFile,HeadingStr,lstrlen(HeadingStr),0,NULL);
CloseHandle(myFile);

如何解决此错误?注意 我有写入权限。我也以管理员的身份运行。请注意,我已经在上一个代码中使用之外定义了 BufferNo,myFile。

更新我删除了body=""现在我得到

filename.exe triggered a breakpoint.

指向下面评论中提到的文件。

**EDIT**

现在,我在写作时遇到了问题。错误读取

Unhandled exception at 0x7c811384 in stabilo.exe: 0xC0000005: Access violation writing location 0x00000000 on.

和点到线

   WriteFile(myFile,body,lstrlen(body),0,NULL);

本节中的变量body不正确。

body="";
...
BufferNo=sprintf(body,"%.5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5fn",
                      a1,a2,a3,a4,a5,a6,a7,a8);

大概这是一个char *(虽然我只是猜测(,这意味着您正在尝试将一堆数字值写入能够容纳零个字符的常量字符串。由于它是一个常量,因此它是不可写的。

将其更改为char body[1000];或类似内容。

您的sprintf肯定会溢出缓冲区。更糟糕的是:你试图写一个字符串文字。