1
只是一個語法問題,這裏是我的代碼片段。 (對不起,瀏覽器是不是讓我正確粘貼到堆棧溢出。)將字符串文字傳遞給'char *'函數並得到編譯器警告
#include <iostream> /* 'iostream.h' is deprecated. */
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std; /* Required. */
FILE *OpenFile(char *Filename)
{
FILE *FP;
if((FP = fopen(Filename, "r")) == NULL)
{ /* Error opening file. */
std::cout << "[!!] Unable to open database!"
<< " Are you sure it exists?\n"
<< "[<<] Database Unchanged.\n";
exit(EXIT_FAILURE); /* End program. */
}
else /* Properly opened the file. */
return FP;
}
int main(void)
{
FILE *Data; /* Our database file pointer. */
Data = OpenFile("Data.txt");
printf("Success!\n");
return 0;
}
當我編譯,我得到以下警告:
$ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:27:28: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
$
我要去哪裏錯了?在C++
請在此處貼你的代碼。不要讓我們訪問外部網站來查看您的代碼。 – 2012-03-03 04:22:16
我知道,firefox對我來說一直很奇怪,試圖找出原因。 – 2012-03-03 04:24:34
報告'cerr'上的錯誤;這就是它的設計目的。理論上講,使用'FILE * Data = OpenFile(「data.txt」);'並在退出之前關閉文件是一個好主意。實際上,在這種情況下,這並不重要,但在許多情況下,不釋放分配的資源是一個壞主意,RAII(資源獲取初始化)建議初始化定義符號。 – 2012-03-03 04:32:59