簡體   English   中英

E0167 錯誤:* 類型的 C++ 參數與“文件**”類型的參數不兼容

[英]E0167 error : C++ argument of type * is incompatible with parameter of type “FILE**”

我對 C 非常陌生,在處理書中的示例時,我不斷收到“* 類型的 C++ 參數與** 類型的參數不兼容”錯誤。 我正在使用 Visual Studio 2019 C++。 這是我使用 fopen_s 時出現錯誤的地方:

#include<stdio.h>
#include<stdlib.h>

int main()
{
FILE *inFile;

inFile = fopen_s("prices.dot","r"); /*Here is the line with an error*/

if (inFile == NULL)
{
    printf("\nThis file does not existL");
    printf("\nPlease make sure that this file currently exist");
    exit(1);

}

printf("\nThe file has been succfully open for reading.");

return(0);
}

當我使用“fopen”時,我收到一個警告,告訴我使用 fopen_s,當我使用 fopen_s 時,我得到另一個錯誤。 我想知道我是否可以在這個問題上得到任何幫助。 謝謝!

如果你 google "MSDN fopen_s" 並閱讀 Microsoft Developer Network 上的文檔,你會發現 function 原型與 fopen() 不同:

errno_t fopen_s(
   FILE** pFile,
   const char *filename,
   const char *mode
);

這意味着您在該區域的代碼將更改為:

FILE *inFile;
errno_t errcode;

errcode = fopen_s(&inFile, "prices.dot","r"); 

if (errcode != 0) { /* do error handling, perhaps quit */ }

或者,您可以繼續編寫舊式 C 代碼,只需使用“fopen()”並在文件頂部關閉 MSVC 編譯器,添加以下#define:

#define _CRT_SECURE_NO_WARNINGS

請注意,fopen_s() 已添加到 C 2011 標准中,並在第 K.3.5.2.1 節中進行了描述——這意味着這仍然是任何現代 Z0D61F83701D412D174E 編譯器上的可移植 C 代碼。

您的書可能早於 2011 年,而這本 function 當時不是 C 的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM