在编程中,我们常常会遇到一些配置文件或初始化文件。这些文件通常后缀名为.ini或者.conf。能够直接用记事本打开。里面会存储一些程序參数,在程序中直接读取使用。比如,计算机与server通信。server的ip地址,段口号能够存储于ini文件里。这样假设我想换另外一台server时。直接将ini文件里的ip地址改变就可以。程序源码不须要做不论什么改动。

本文将分享一段经常使用代码,用于读取配置文件里的信息。本文中的代码为C语言编写,在ubuntu 12.04 linux系统中调试没有问题。详细操作例如以下:

1. 首先用记事本创建一个config.ini文件(文件名称能够随便取)。并如果该文件是我们要读取的配置文件。文件内容例如以下:

information1: 1234567890

information2: this is test information

information3: `~!@#$%^&*()_+{}-[]\|:"/.,<>

如果我们读取的初始化文件每一行都是  <属性名称>: <属性值>  的格式。在上述样例中,文件共同拥有三行,分别代表三个属性的信息。

2. 然后就是我们的代码文件了,例如以下(将下面代码存在ReadFile.cpp中):

#include

#include

const size_t MAX_LEN = 128;

typedef struct{

char firstline[MAX_LEN];

char secondline[MAX_LEN];

char thirdline[MAX_LEN];

} Data;

void readfile(Data *d){

const char *FileName = "config.ini";

char LineBuf[MAX_LEN]={0};

FILE *configFile = fopen(FileName, "r");

memset(d,0,sizeof(Data));

while(NULL != fgets(LineBuf, sizeof(LineBuf), configFile))

{

size_t bufLen = strlen(LineBuf);

if('\r' == LineBuf[bufLen-1] || '\n' == LineBuf[bufLen-1])

{

LineBuf[bufLen-1] = '\0';

}

char *pos = strchr(LineBuf,':');

if(NULL != pos)

{

*pos = '\0';

pos++;

if(0 == strcmp(LineBuf, "information1"))

{

for(; *pos == ' '; pos++){}

strcpy(d->firstline, pos);

}

else if(0 == strcmp(LineBuf, "information2"))

{

for(; *pos == ' '; pos++){}

strcpy(d->secondline, pos);

}

else if(0 == strcmp(LineBuf, "information3"))

{

for(; *pos == ' '; pos++){}

strcpy(d->thirdline, pos);

}

else

{

printf("Failed to read information from the file.");

break;

}

}

}

fclose(configFile);

configFile = NULL;

return;

}

int main(int argc, char *argv[])

{

Data *d = new Data;

readfile(d);

printf("d->firstline is \"%s\"\n", d->firstline);

printf("d->secondline is \"%s\"\n", d->secondline);

printf("d->thirdline is \"%s\"\n", d->thirdline);

delete d;

return 0;

}

当中,struct Data是用于存储要读取的信息的结构体,readfile函数也就是实现我们读取功能的函数,当中的值均存在struct Data中。最后我们写了一个简单的main函数用来測试结果。须要注意的是,在struct Data中,我们设置了char数组长度,最大不超过128。因此假设要读取的信息超过128字节可能会出错。假设有须要读取更长的话能够将MAX_LEN设置为一个更大的值。

3. 最后就是我们的调试结果了,在命令行中执行例如以下命令

$ g++ -o test.out ReadFile.cpp

$ ./test.out

然后就是执行结果:

d->firstline is "1234567890"

d->secondline is "this is test information"

d->thirdline is "`!@#$%^&*()_+{}-[]\|:"/.,<>"

这样的读取文件的代码应该很经常使用。要掌握。

查看原文