fopen_s 未在 scope(C++:codeblocks ) 中声明

fopen_s was not declared in the scope(C++:codeblocks)

本文关键字:codeblocks 声明 C++ 未在 scope fopen      更新时间:2023-10-16

我是初学者,尝试使用 fopen_s 读取文件,但错误显示

Build: Debug in histimg (compiler: GNU GCC Compiler) ===|
E:CCShistimgmain.cpp|19|error: 'fopen_s' was not declared in this scope|

法典:

fopen_s(&fp,"C:\Users\asus\Pictures\SavedPictures\1.png", "rb");
if (fp == 0)
{
cout << "Open image failed!" << endl;
exit(0);
}

你能指导我如何解决它吗?谢谢。

fopen_s

不是C++标准库的一部分。它是特定于 MSVC 的扩展或 C 标准库的可选部分。

如果您的编译器不支持它作为扩展,则无法使用它。

使用C++文件流标准库<fstream>,即std::fstream相反。

根据https://en.cppreference.com/w/c/io/fopen

与所有边界检查函数一样,fopen_s只能保证 如果实现定义了STDC_LIB_EXT1并且如果 用户定义整数常量 1 的STDC_WANT_LIB_EXT1在包括 stdio.h 之前。