C语言读写配置文件

CException.h
/************************************************************************/
/*             make0000@msn.com       */
/************************************************************************/
/************************************************************************/
#include "stdio.h"       
#include "conio.h" 
#include "signal.h"      
#include "setjmp.h"
#include "assert.h"
#ifdef __cplusplus
#include "iostream"              
#include "exception"
extern "C"{
#define dllexport __declspec(dllexport)            
jmp_buf Jmp_Buf; 
int E; 
#define Exception 0x00000 
#define e Exception 
#define try if(!(E=setjmp(Jmp_Buf))) 
#define last_error() E 
#define catch(val) else 
#define throw(val) longjmp(Jmp_Buf,val)     
#define check(expersion) assert(expersion)
#define GetError() errno      
dllexport void sig_usr(int);
dllexport char* getTime();   
}                
#else
#define dllexport __declspec(dllexport)            
jmp_buf Jmp_Buf; 
int E; 
#define Exception 0x00000 
#define e Exception 
#define try if(!(E=setjmp(Jmp_Buf))) 
#define last_error() E 
#define catch(val) else 
#define throw(val) longjmp(Jmp_Buf,val)     
#define check(expersion) assert(expersion)
#define GetError() errno      
dllexport void sig_usr(int);
dllexport char* getTime();
              
#endif 


File.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define SIZE 128
#include "CException.h"
#define export __declspec(dllexport)
//读取配置文件.
int read_file(char* filename,char* key,char* value);
//写配置文件.
int write_file(char* filename,char* key,char* value);
//释放文件.
int release();
//写入节.
int write_section(char* filename,char* section);
int read_section(char* filename);
int getAuthor(char* value);
void getVersion(char* value);

File.c
#include "File.h"
#include <string.h>

int read_file(char* filename,char* key,char* value)
{
int flag=0;
char buffer[SIZE];
FILE *file=fopen(filename,"r");
try
   {
    if(file==NULL)
    {
     flag=1;
     throw(flag);
    }
    else
    {
     while(fgets(buffer,SIZE,file)!=NULL)
     {
      int i=0,j=0,len=strlen(key);
      while(buffer[i]!='\0')
      {
        if(buffer[i]=='$'&&buffer[i+len+1]=='=')
        {
         j=i+len+2;
          while(buffer[j]!='\0'&&buffer[j]!=';')
          {
           int h=0;
           if(buffer[i+1]==key[i])
           {
            //printf("%c",buffer[j]);
            value[j-i-len-2]=buffer[j];
           }
           j++;
          }
         break;
        }
        else if(buffer[i]=='/'&&buffer[i+1]=='/'||buffer[i]==';')
        {
         break;
         //comment
        }
        
       
      i++;
      }
     }
    }
   }
   catch(Exception)
   {
    flag=2;
    fclose(file);
    printf("can't open file %s",filename);
    exit(1);
   }
fflush(file);
fclose(file); 
return flag;

}

int write_file(char* filename,char* key,char* value)
{
int flag=0;
FILE* file;
file=fopen(filename,"a");
try
{
   if(file==NULL)
   {
   flag=1;
   throw(flag);
   }
   fprintf(file,"$%s=%s\n",key,value);
}
catch(Exception)
{ 
   printf("Can't write file %s",filename);
   exit(1);
}
fflush(file);
fclose(file);
return flag; 
}

int write_section(char* filename,char* section)
{
int flag=0;
FILE* file=NULL;
try
{
   file=fopen(filename,"a");
   if(file!=NULL)
   {
    fprintf(file,"[%s]\n",section);
   }
   else
   {
    int flag=1;
    throw(flag);
   }
}
catch(Exception)
{
   printf("can't open file %s",filename);
   exit(0);
}
fflush(file);
fclose(file);
return flag; 
}
int release()
{
int flag=1;
return flag;
}
int read_section(char* filename)
{
return 0;
}
int getAuthor(char* value)
{
char author[128]="武汉软件工程职业学院计算机应用系孟德军";
int i=0;
for(i=0;i<strlen(author);i++)
{
value[i]=author[i];
}
return 0;
}
void getVersion(char* value)
{
char version[128]="2009//05//01";
int i=0;
for(i=0;i<strlen(version);i++)
{
value[i]=version[i];
}

}
/**************************************************************************
void main()
{
char* str=NULL;
char author[120];
char buffer[128];
char buffer1[128];
char buffer2[128];
read_file("F:\\exercise\\C++!C\\sys.ini","password",buffer);
read_file("F:\\exercise\\C++!C\\sys.ini","username",buffer1);
read_file("F:\\exercise\\C++!C\\sys.ini","driver",buffer2);
printf("password=%s\n",buffer);
printf("\n");
printf("username=%s\n",buffer1);
printf("\n");
printf("driver=%s\n",buffer2);
getAuthor(author);
printf("\n");
printf("author=%s",author);
release();
}
****************************************************************************/

sys.ini

[mysql]
$username=root
$password=123456
$driver=com.mysql.jdbc.Driver
$url=jdbc:mysql://localhost:3306/mysql

动态链接库测试:

#include <windows.h>
typedef (* write)(char* filename,char* key,char* value);
typedef (* writes)(char* filename,char* value);
typedef (* read)(char* filename,char* key,char* value);
HINSTANCE hInstance;
write write_file;
writes write_section;
read read_file;
char buffer[128];
char buffer1[128];
void main()
{
hInstance=LoadLibrary("File.dll");
if(hInstance!=NULL)
{

write_file=(write)GetProcAddress(hInstance,"write_file");
write_section=(writes)GetProcAddress(hInstance,"write_section");
read_file=(read)GetProcAddress(hInstance,"read_file");

write_section("config.ini","mysql");
write_file("config.ini","username","root");
write_file("config.ini","password","admin");
write_file("config.ini","driverClassName","com.mysql.jdbc.Driver");
write_file("config.ini","url","jdbc:mysql://localhost:3306/user");

read_file("config.ini","username",buffer);
printf("username=%s",buffer);

read_file("config.ini","password",buffer1);
printf("password=%s",buffer1);
}
else
{
printf("can't load dynamic file File.dll");
}
FreeLibrary(hInstance);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux C语言中,配置文件通常使用INI文件格式。INI文件格式是一种简单的文本文件格式,其中键值对以节(section)的形式组织,并使用方括号([])将节名括起来。 以下是一个示例INI文件: ``` [Section1] key1=value1 key2=value2 [Section2] key3=value3 key4=value4 ``` 使用C语言取INI文件的步骤如下: 1. 打开INI文件入内容。 2. 解析INI文件内容,将键值对存储到内存中的数据结构中。 3. 使用存储的键值对执行相应的操作。 以下是一个简单的例子: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_LEN 1024 #define MAX_SECTION_LEN 256 #define MAX_KEY_LEN 256 #define MAX_VALUE_LEN 256 typedef struct { char section[MAX_SECTION_LEN]; char key[MAX_KEY_LEN]; char value[MAX_VALUE_LEN]; } config_item_t; int parse_config_file(const char* filename, config_item_t** items, int* count) { FILE* fp = fopen(filename, "r"); if (!fp) { return -1; } char line[MAX_LINE_LEN]; config_item_t* item = NULL; int item_count = 0; while (fgets(line, MAX_LINE_LEN, fp)) { // 去掉行末换行符 char* p = strchr(line, '\n'); if (p) *p = '\0'; // 去掉行首空格 p = line; while (*p == ' ') ++p; // 解析节 if (*p == '[') { char* q = strchr(p, ']'); if (!q) { fclose(fp); return -1; } *q = '\0'; item = NULL; continue; } // 解析键值对 if (item) { char* q = strchr(p, '='); if (!q) continue; // 忽略无效行 *q = '\0'; strcpy(item->key, p); strcpy(item->value, q + 1); ++item; } else { // 新建配置项 item = (config_item_t*)realloc(item, (item_count + 1) * sizeof(config_item_t)); if (!item) { fclose(fp); return -1; } strcpy(item->section, p); item->key[0] = '\0'; item->value[0] = '\0'; items[item_count++] = item; } } fclose(fp); *count = item_count; return 0; } void free_config_items(config_item_t** items, int count) { for (int i = 0; i < count; ++i) { free(items[i]); } free(items); } int main() { config_item_t* items[100]; int count = 0; if (parse_config_file("test.ini", items, &count) == 0) { for (int i = 0; i < count; ++i) { printf("[%s]\n", items[i]->section); printf("%s=%s\n", items[i]->key, items[i]->value); } } free_config_items(items, count); return 0; } ``` 该程序使用`parse_config_file`函数取INI文件并解析内容,将每个节的键值对存储到一个`config_item_t`结构体中。最后,在`main`函数中遍历所有配置项并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值