【问题标题】:fscanf_s not works properly in Winserver2008 VS2013 64bitfscanf_s 在 Winserver2008 VS2013 64bit 中无法正常工作
【发布时间】:2015-07-02 17:16:24
【问题描述】:

当我们在Visual Studio 2013(C++控制台应用程序)、Os-Winsever2008(64位)中使用fscanf_s方法时,文件指针会提前一到两个字节读取数据。 例如:读取文本文件时,文件的第二行是“Administrator”,但 fscanf_s() 返回单词为“dministrator”。 请帮我纠正这个问题。 该代码在使用 Visual Studio 2008 的 32 位 Windows XP 上运行良好。

FILE* pFile;
pFile = NULL;
string strFile = "E:\\10_Products.lrf";
fopen_s(&pFile, strFile.c_str(), "r");
char szTemp[256];
string strTemp = "";
if (NULL != pFile)
{   
    while (!feof(pFile))
    {
        nRet = fscanf_s(pFile, "%s", szTemp);
        if (EOF == nRet)
        {
            cout << "EOF detected";
        }
    }
    return 0;
}

10_Products.lrf文件的格式如下。

[OPERATOR_LEVEL]
Administrator

【问题讨论】:

  • 示例代码 FILE* pFile; pFile = NULL;字符串 strFile = "E:\\10_Products.lrf"; fopen_s(&pFile, strFile.c_str(), "r");字符 szTemp[256];字符串 strTemp = ""; if (NULL != pFile) { while (!feof(pFile)) { nRet = fscanf_s(pFile, "%s", szTemp); if (EOF == nRet) { cout
  • 你应该编辑你的帖子而不是把它添加为评论
  • 如果使用 fscanf 这个代码可以正常工作。
  • 对于 fscanf_s 它显示了这个问题,请帮助找到问题

标签: c++ visual-c++ visual-studio-2013 64-bit windows-server-2008-r2


【解决方案1】:

来自 fscanf_s() 上的文档,http://msdn.microsoft.com/en-us/library/6ybhk9kc.aspx

The main difference between the secure functions (with the _s suffix) and the older functions is that the secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.

还有http://msdn.microsoft.com/en-us/library/w40768et.aspx

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:

char s[10];

scanf("%9s", s, 10);

所以你应该这样称呼它:

fscanf_s(fp,"%80s",cmd, sizeof(cmd));

【讨论】:

  • 如果这个答案对你有帮助,你能把它标记为答案吗?
猜你喜欢
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 2021-04-08
相关资源
最近更新 更多