本來想整合一下,這兩天學習的內容,發佈一個具備讀寫ini、寫日誌的demo。誰想到新的問題出來,按照原思路。創建common.cpp 將一些工具方法歸納其中。demo1.cpp通過引入common.h文件 實現方法的調用。
關鍵代碼如下:
common.cpp:
/** * 這是一個 工具類的對象 */#include <windows.h>#include <string.h>#define INI_FILE_PATH ".//study.ini"#define LOG_FILE_NAME "studylog"#define LOG_VALUE_MAXSIZE 80void readIniValue(char* lable,char* anchor,char* iniValue){ char buffer[LOG_VALUE_MAXSIZE]; GetPrivateProfileString(lable, anchor, NULL, buffer, sizeof(buffer), INI_FILE_PATH );//讀取 配置文件 需要引入 <windows.h> iniValue = new char[sizeof(buffer)+1]; strcpy(iniValue,buffer); }
commom.h:
#ifndef COMMON #define COMMON /** * 讀取配置文件 * @param label 日誌文件 標籤 * @param anchor 日誌文件 錨點 * @param iniValue 讀取到日誌的值 */ void readIniValue(char* lable,char* anchor,char* iniValue); #endif
demo1.cpp:
/* 這個案例 用於說明C++ 讀取配置文件 寫入日誌文件 2017-08-03 *//** * 這個案例 用於說明C++ 讀取配置文件 寫入日誌文件 * @param argc [description] * @param argv [description] * @return [description] * @createTime 2017-08-03 */ #include "common.h"#include <iostream>#include <windows.h>// 讀取ini文件 引入 必要文件using namespace std;//定義全局變量// #define INI_FILE_PATH ".//study.ini"// #define LOG_FILE_NAME "studylog.txt"// void readIniValue(char* lable,char* anchor,char* iniValue){// char buffer[80];// GetPrivateProfileString(lable, anchor, NULL, buffer, sizeof(buffer), INI_FILE_PATH );//讀取 配置文件 需要引入 <windows.h>// std::cout << buffer << '\n';////// iniValue = new char[sizeof(buffer)+1];// strcpy(iniValue,buffer);//// }int main() { // 整理 讀取 配置ini 配置文件 // char buffer[80]; // GetPrivateProfileString("Log", "Level", NULL, buffer, sizeof(buffer), INI_FILE_PATH );//讀取 配置文件 需要引入 <windows.h> // cout<<buffer; char* iniValue=0; char* lable="Log"; char* anchor="Level"; //lable="Log"; //anchor="Level"; readIniValue(lable,anchor,iniValue); std::cout << iniValue << '\n'; return 0; }
編譯時 提示如下錯誤信息:
demo1.cpp: In function 'int main()':
demo1.cpp:38:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
demo1.cpp:39:15: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\ADMINI~1\AppData\Local\Temp\cclOz7l9.o:demo1.cpp:(.text+0x3e): undefined reference to `readIniValue(char*, char*, char*)'
collect2.exe: error: ld returned 1 exit status
從錯誤信息中分析,應該是頭文件沒有起作用。可是參考相關一些相關頭文件的使用。未發現問題之所在。還請哪位大俠給予幫助