任何人都可以告诉我为什么这不是打开文件进行错误检查? [英] can any one tell me why this is not opening file for error check ?

查看:59
本文介绍了任何人都可以告诉我为什么这不是打开文件进行错误检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;

int main()
{
  errno_t err;

  err = fopen_s(&pg, "input.dat", "r");
  if (err == 0)
  {
    printf("The file 'input.dat' was opened\n");
  }
  else
  {
    printf("The file 'input.dat' was not opened\n");
  }
  return 0;
}

推荐答案

可能是任何东西!



最有可能的是它不存在。

由于你根本没有指定路径,它会尝试在当前目录中打开它,这通常是应用程序启动的文件夹,但不一定是。

所以将文件复制到一个特定的文件夹:C:\Temp \ input.dat并确保文件夹和文件都对所有用户都有足够的访问权限。然后尝试访问它指定整个路径。



如果这不能解决它,那么看看fopen_s返回的值 - 这是一个你可以使用的错误代码找出调用失败的原因。
Could be anything!

The most likely is that it doesn't exist.
Since you don't specify a path at all, it will try to open it in the "current directory" which will normally be the folder the app started in, but doesn't have to be.
So copy the file into a specific folder: "C:\Temp\input.dat" and make sure that the folder and file both have adequate access permissions to all users. Then try to access it specifying the whole path.

If that doesn't solve it, then look at the value fopen_s returns - it's an error code which you can use to find out why the call failed.


正如文档明确指出:

As the documentation clearly states:
如果发生错误,则返回错误代码并设置全局变量errno。有关更多信息,请参阅errno,_doserrno,_sys_errlist和_sys_nerr。
If an error occurs, the error code is returned and the global variable errno is set. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.


感谢您的帮助......我非常感谢
thanks for your help... i appreciate this a lot


这篇关于任何人都可以告诉我为什么这不是打开文件进行错误检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆