本来想整合一下,这两天学习的内容,发布一个具备读写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 80
void 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
从错误信息中分析,应该是头文件没有起作用。可是参考相关一些相关头文件的使用。未发现问题之所在。还请哪位大侠给予帮助