C语言读写配置文件的方法

 更新时间:2020年4月25日 17:38  点击:1819

本文实例讲述了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(); 
}

希望本文所述对大家的C语言程序设计有所帮助。

[!--infotagslink--]

相关文章

  • C语言实现放烟花的程序

    这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
  • C语言中的字符(char)详细讲解

    本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
  • 详解如何将c语言文件打包成exe可执行程序

    这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • 查找php配置文件php.ini所在路径的二种方法

    通常php.ini的位置在:复制代码 代码如下:/etc目录下或/usr/local/lib目录下。如果你还是找不到php.ini或者找到了php.ini修改后不生效(其实是没找对),请使用如下办法:1.新建php文件,写入如下代码复制代码 代码如下:<?phpe...2014-05-31
  • C语言中计算正弦的相关函数总结

    这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
  • 详解C语言中的rename()函数和remove()函数的使用方法

    这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
  • C语言中求和、计算平均值、方差和标准差的实例

    这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
  • php cli配置文件问题分析

    引言今天在教别人使用protobuf的时候,无意中发现了一个php cli模式下的诡异问题,费了老半天的找到解决方法了,这里拿出来分享下。问题描述我们这边最先引入了protobuf协议,使用的是allegro/php-protobuf这个扩展安装的。...2015-10-21
  • C#中读写INI配置文件的方法

    这篇文章主要介绍了C#中读写INI配置文件的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C语言的基本语法详解

    本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
  • C语言中send()函数和sendto()函数的使用方法

    这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
  • C语言实现从文件读入一个3*3数组,并计算每行的平均值

    今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
  • 使用C#实现读取系统配置文件的代码实例讲解

    这篇文章主要介绍了使用C#实现读取系统配置文件的代码实例,使用到了ConfigurationManager类,需要的朋友可以参考下...2020-06-25
  • C语言中memcpy 函数的用法详解

    这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
  • 使用C语言操作文件的基本函数整理

    这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
  • C语言中查找字符在字符串中出现的位置的方法

    这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
  • C语言菜鸟基础教程之a++与++a

    很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
  • 详解SpringBoot读取配置文件的N种方法

    这篇文章主要介绍了详解SpringBoot读取配置文件的N种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 详解C语言中const关键字的用法

    这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25