C++ 实践之引入外部头文件

    上周发了一个有关C++引入头文件失败的案例。回头仔细确认一下相关的代码,还是没有确认相关问题。偶然机会公司同事交流技术问题,无意间提示了我。是否因为开发IDE的问题,导致文件为编译。接下来又是一番大动作。为了兼容VC++ 6.0 将我的开发本的操作系统由Win10 修改为Win7 (cool !还好之前积累了丰富重装系统的经验)。按照sublime text3 编辑+VC++ 编译的开发模式(有些古怪。因为sublime Text3 具备 较好的插件,能够保证代码的开读性),最终实现成功。实现代码如下:

    main.cpp

#include <iostream>


#include "common.h"

using namespace std;

int main() {
	cout << "************************************" << "\n";
	cout << "这是 study 项目" << "\n";
	cout << "************************************" << "\n";
	char* iniValue = "abc";
	char* iniValue1 = "value";

	//读取日志 成功
	char* lable = "Log";
	char* anchor = "Level";
	

	readIniFile(lable, anchor, iniValue1);

	cout << "************************************" << "\n";
	cout << "这是 readIniFile 方法运行" << "\n";
	cout << "返回值:iniValue1" << iniValue1 << "\n";
	
	cout << "************************************" << "\n";


	return 0;
}


    common.cpp

#include <iostream>

#include <string.h>

#include <windows.h>


using namespace std;

#define INI_FILE_PATH "./study.ini"
#define LOG_VALUE_MAXSIZE 80

void readIniFile(char* lable, char* anchor,  char* &contentValue) {
	
	char buffer[LOG_VALUE_MAXSIZE];
	GetPrivateProfileString(lable, anchor, NULL, buffer, sizeof(buffer), INI_FILE_PATH );//读取 配置文件 需要引入 <windows.h>
	contentValue = new char[sizeof(buffer) + 1];
	strcpy(contentValue, buffer);

	



}

    common.h


  #ifndef COMMON
  #define COMMON
  
    /**
    * readIniFile  读取日志文件
    *
    */
    void readIniFile(char* , char*, char* &);

  #endif

    运行结果如下:

************************************
这是 study 项目
************************************
************************************
这是 readIniFile 方法运行
返回值:iniValue1:log1
************************************

    总结经验教训:

  1. 过于盲目使用文本IDE(sublime test3 未对所有文件进行编译);
  2. 开发本使用操作系统较新,影响部分IDE的使用(windows 您到是向下兼容做的到位点啊);
  3. 对于C++基础知识掌握不扎实。后期仍然需要继续努力;

    

转载于:https://my.oschina.net/u/3435444/blog/1504049

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值