在 Visual Studio 中写入文件时出错
Error Writing to File in Visual Studio
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
肯定会溢出缓冲区。更糟糕的是:你试图写一个字符串文字。
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 在 C/C++ 中打开驻留在 "/sys/" 下的文件时出错
- 在头文件中定义变量不会出错
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 生成文件中隐式规则中的 -c 标志出错
- 编译时出错 - 链接.cpp和头文件
- 从人员矢量插入和读取文件时出错
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 猫鼬 - 包含标准C++库文件时出错
- 在f2c.h文件中定义min()max()宏时出错
- ASIOSDK;从hostsample.cpp生成mex文件时出错
- 使用emscripten将c++文件转换为wasm时出错
- 编译后可执行文件出错
- 头文件C++出错
- ftell 非常大的文件出错
- 从磁盘读取映像文件出错