C++ 为什么ftscanf抛出异常而ftscanf不抛出异常?
我有以下输入文件:C++ 为什么ftscanf抛出异常而ftscanf不抛出异常?,c++,c,windows,exception,unicode,C++,C,Windows,Exception,Unicode,我有以下输入文件: 1 100000 hajs ssaa 25 2 150000 sdsd Assso 22 ... 以及以下节目: #define _UNICODE #define UNICODE #define _CRT_SECURE_NO_WARNINGS #include <Windows.h> #include <tchar.h> #include <stdio.h> #include <stdlib.h> #define MA
1 100000 hajs ssaa 25
2 150000 sdsd Assso 22
...
以及以下节目:
#define _UNICODE
#define UNICODE
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARS 30
typedef struct student
{
WORD identifier;
DWORD registerNumber;
TCHAR surname[MAX_CHARS + 1];
TCHAR name[MAX_CHARS + 1];
WORD mark;
} student_t, *student_ptr;
int _tmain(DWORD argc, LPTSTR argv[])
{
FILE* fileIn;
errno_t error;
student_t student;
#ifdef UNICODE
error = _wfopen_s(&fileIn, argv[1], L"r");
#else
error = fopen_s(&fileIn, argv[1], "r");
#endif
if (error)
{
_ftprintf(stderr, _T("Unable to open %s\n"), argv[1]);
exit(EXIT_FAILURE);
}
while (_ftscanf_s(fileIn, _T("%hu %lu %s %s %hu"), &student.identifier, &student.registerNumber,
student.surname, student.name, &student.mark) != EOF)
{
}
fclose(fileIn);
return 0;
}
而如果我使用
\u ftscanf
(不安全版本),程序运行时不会出错。为什么?您必须指定%s
所需的长度参数。请尝试使用宏:
很可能是因为您忘了为函数提供
%s
所需的长度参数。
First-chance exception at 0x53871CA4 (msvcr110d.dll) in vsproj.exe: 0xC0000005: Access violation writing location 0x002F0000.
while (_ftscanf_s(fileIn, _T("%hu %lu %s %s %hu"), &student.identifier, &student.registerNumber,
student.surname, _countof(student.surname), student.name, _countof(student.name), &student.mark) != EOF)
{
}