我有一个简单的 C 代码如下:
#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <windows.h>
#include <tchar.h>
int main()
{
int ndsc;
char *line = ' ';
FILE *fd;
int x;
wchar_t path[256];
wcscpy(path, L"C:/");
//wcscat(path, L"common\\descr.txt", wcslen(L"common\\descr.txt"));
//Previous
wcscat(path, L"common/descr.txt");
if ((fd = _wfopen(path, L"r")) == NULL)
{
printf("Can't open %s for reading.\n", path);
return 1;
}
for (ndsc = 0; fgetws((wchar_t*)line, 80, fd) != NULL; ndsc++)
{
x = wcslen((const wchar_t*)line);
printf("Length of %d line is %d\n", ndsc, x);
}
fclose(fd);
return 0;
}
我正在尝试从 C:\Common 目录中读取名为 descr.txt 的文件。但在 for 循环中它会抛出未处理的异常,如下所示:
任何人都可以解释一下为什么我会收到此错误以及如何解决。 顺便说一句,此代码正在 AutoCad 的 .arx 文件中使用。我刚刚提取了引发错误的部分,并尝试将其作为 VS 2015 Update 3 中的独立项目运行。此代码是 .NET 调用的 .C 文件的一部分。即使在 .arx 上下文中执行(从 AutoCad 单击所需菜单)也会出现相同的异常。
提前致谢。
最佳答案
此处:char *line = ' ';
您将 char
分配给 char *
。
稍后您尝试使用宽字符串缓冲区。您需要 wchar_t line[80]
来实际为 80 个 wchar_t
分配空间。
关于c - C中的异常读取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45505948/