#include <stdio.h>
#include <stdlib.h>
...
int function () {
FILE *f;
f = fopen("list.txt", "rb+");
...
int value;
if (_chsize_s(f, value) == 0) return 1;
}
出于某种原因,上面的代码片段在 MS Visual Studio 中运行时会在 _chsize_s() 函数处生成错误。弹出以下消息:
Debug Assertation Failed!
Expression: (fh >= 0 && (unsigned)fh < (unsigned)_nhandle)
我不知道这是怎么回事,更不用说如何解决了。任何帮助将不胜感激。
您忽略了编译器的警告或将其禁用。为了修复隐式声明警告,您必须包含 _chsize_s
的 header :
#include <io.h>
这将修复隐式声明警告,并发现错误,即 _chsize_s
被错误的类型调用。正确的调用方式是:
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
...
int function(void) {
FILE *f;
f = fopen("list.txt", "rb+");
...
int value;
if (_chsize_s(_fileno(f), value) == 0) return 1;
}
这表明不忽略编译器警告是多么重要(除了异常(exception)......大多数理智的人在用 MSVC 编写 C 时会定义 _CRT_SECURE_NO_WARNINGS
)。
我是一名优秀的程序员,十分优秀!