gpt4 book ai didi

c - _chsize 在 vi​​sual studio 2017 上引发断言错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:06 24 4
gpt4 key购买 nike

#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)。

关于c - _chsize 在 vi​​sual studio 2017 上引发断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com