_tfopen出现错误22的可能原因是什么?
What are the possible reasons to have an error 22 with _tfopen?
我向客户提供了一个使用 Visual C++ 开发的应用程序,该应用程序在我的环境中运行良好。不幸的是,我的客户在调用 _tfopen
时收到错误 22。
下面是一个与我在此应用程序中编写的代码类似的小片段:
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include "tchar.h"
FILE* fp;
int openFile(const TCHAR* p, const TCHAR* r)
{
errno_t err = 0;
fp = _tfopen(p, r);
if (!fp)
{
err = errno;
_tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);
}
else
{
printf("All is ok\n");
}
return err;
}
int main()
{
openFile(_T("\\127.0.0.1\hidden_folder$\folder\video_file.mxf"), _T("rb"));
return 0;
}
我的客户得到:
Error 22, can't open file: \127.0.0.1\hidden_folder$\folder\video_file.mxf with rights rb
错误 22 表示 EINVAL
参数无效。但是,就我而言,参数是正确的(根据日志)。
这种行为的原因是什么?
我尝试了很多方法来重现此错误:删除 video_file.mxf、更改文件位置、更改文件权限...没关系,我从来没有得到错误 22.
备注:
- 我知道
_tfopen
现在已经弃用了,我也创建了一个使用 _tfopen_s
的版本,问题仍然存在。
- 它不是 this question 的副本,因为在我的例子中,我没有错误,我想重现它(以了解如何更正它)。
我按照@Michael 的想法将我的源代码更改为这个:
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include "tchar.h"
FILE* fp;
int openFile(const TCHAR* p, const TCHAR* r)
{
errno_t err = 0;
fp = _tfopen(p, r);
if (!fp)
{
err = errno;
_tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);
HANDLE hFile;
hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf(_T("GetLastError = %d"), GetLastError());
}
else
{
_tprintf(_T("No error with CreateFile"));
CloseHandle(hFile);
}
}
else
{
printf("All is ok\n");
}
return err;
}
int main()
{
openFile(_T("\\127.0.0.1\hidden_folder$\folder\video_file.mxf"), _T("rb"));
return 0;
}
想法是继续使用_tfopen
得到一个FILE
指针(在我的应用中用了很多时间)但是现在使用CreateFile
得到一个更精确的指针错误代码。它工作得很好,因为我得到了 error 1326:
The user name or password is incorrect.
所以,现在,我可以断定 _tfopen
在无法登录到远程文件服务器时会抛出错误 22。此错误与错误 13(权限被拒绝)不同,因为错误 13 表示用户已正确登录。
我向客户提供了一个使用 Visual C++ 开发的应用程序,该应用程序在我的环境中运行良好。不幸的是,我的客户在调用 _tfopen
时收到错误 22。
下面是一个与我在此应用程序中编写的代码类似的小片段:
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include "tchar.h"
FILE* fp;
int openFile(const TCHAR* p, const TCHAR* r)
{
errno_t err = 0;
fp = _tfopen(p, r);
if (!fp)
{
err = errno;
_tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);
}
else
{
printf("All is ok\n");
}
return err;
}
int main()
{
openFile(_T("\\127.0.0.1\hidden_folder$\folder\video_file.mxf"), _T("rb"));
return 0;
}
我的客户得到:
Error 22, can't open file: \127.0.0.1\hidden_folder$\folder\video_file.mxf with rights rb
错误 22 表示 EINVAL
参数无效。但是,就我而言,参数是正确的(根据日志)。
这种行为的原因是什么?
我尝试了很多方法来重现此错误:删除 video_file.mxf、更改文件位置、更改文件权限...没关系,我从来没有得到错误 22.
备注:
- 我知道
_tfopen
现在已经弃用了,我也创建了一个使用_tfopen_s
的版本,问题仍然存在。 - 它不是 this question 的副本,因为在我的例子中,我没有错误,我想重现它(以了解如何更正它)。
我按照@Michael 的想法将我的源代码更改为这个:
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include "tchar.h"
FILE* fp;
int openFile(const TCHAR* p, const TCHAR* r)
{
errno_t err = 0;
fp = _tfopen(p, r);
if (!fp)
{
err = errno;
_tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);
HANDLE hFile;
hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf(_T("GetLastError = %d"), GetLastError());
}
else
{
_tprintf(_T("No error with CreateFile"));
CloseHandle(hFile);
}
}
else
{
printf("All is ok\n");
}
return err;
}
int main()
{
openFile(_T("\\127.0.0.1\hidden_folder$\folder\video_file.mxf"), _T("rb"));
return 0;
}
想法是继续使用_tfopen
得到一个FILE
指针(在我的应用中用了很多时间)但是现在使用CreateFile
得到一个更精确的指针错误代码。它工作得很好,因为我得到了 error 1326:
The user name or password is incorrect.
所以,现在,我可以断定 _tfopen
在无法登录到远程文件服务器时会抛出错误 22。此错误与错误 13(权限被拒绝)不同,因为错误 13 表示用户已正确登录。