errno_t fopen_s( FILE** pFile, const char *filename, const char *mode );
说明:pFile 文件指针将接收到打开的文件指针指向的指针
Infilename 文件名
Inmode 允许的访问类型。
返回值:如果成功返回0,失败则返回相应的错误代码。
#include
#include
using namespace std;
int main()
{
FILE *fin;
errno_t err_src_name;
char src_name[128];
cout << "输入源文件名:" << endl;
cin >> src_name;
// FILE*stream,*stream2;
//Openforread(willfailiffile"crt_fopen_s.c"doesnotexist)
// err = fopen_s(&stream, "crt_fopen_s.c", "r");
err_src_name = fopen_s(&fin, src_name, "r");
if (err_src_name != 0)
{
cout << "打开文件失败" << endl;
return 0;
}
}
注释:
errno_t是一种数据类型,实际上是一个整形,代表错误号码,如果是0就代表没有错误,其他代表抛出一个错误。
头文件:#include 或 #include
下面来一段英文:
error: use of undeclared identifier 'errno_t' // 不清楚的错误
#include
int main(void)
{
errno_t e;
return 0;
}
Which surprisingly raises this error: // 不可预知的错误
main.c:5:5: error: use of undeclared identifier 'errno_t'
errno_t x;
I started to follow the traces: when the compiler sees the <...> inclusions it will first look at /usr/include where of course I found errno.h file. Actually it has a single line in it, besides the license comment, which is: // 根据编译器查找错误来源
#include
Now, at /usr/include/sys in errno.h I found the following lines:
#include
#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1
#include
#endif
And at /usr/include/_types in _errno_t.h I found this:
typedef int errno_t;
So it looks like, it is there, and it is an alias of the integer type, and part of the errno.h -- just as it should be.
// 最终我们可以看到 是一种数据类型
// typedef为C语言关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)
只是记录学习过程,作为参考!