绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
C语言读写配置文件--转载
2020-01-07 18:11:23

http://www.oschina.net/code/snippet_4873_2503

复制代码
[1].[代码] CException.h 跳至 [1] [2] [3]
/************************************************************************/
/*             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 
34
    #define try if(!(E=setjmp(Jmp_Buf))) 
35
    #define last_error() E 
36
    #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
[2].[代码] File.h 跳至 [1] [2] [3]
#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);
[3].[代码] File.c 跳至 [1] [2] [3]

#include "File.h"

#include <string.h>

 

int read_file(char* filename,char* key,char* value)

{

 int flag=;

 char buffer[SIZE];

 FILE *file=fopen(filename,"r");

 try

  {

   if(file==NULL)

   {

    flag=;

    throw(flag);

   }

   else

   {

    while(fgets(buffer,SIZE,file)!=NULL)

    {

     int i=,j=,len=strlen(key);

     while(buffer[i]!='\')

     {

       if(buffer[i]=='$'&&buffer[i+len+]=='=')

       {

        j=i+len+;

         while(buffer[j]!='\'&&buffer[j]!=';')

         {

          int h=;

          if(buffer[i+]==key[i])

          {

           //printf("%c",buffer[j]);

           value[j-i-len-]=buffer[j];

          }

          j++;

         }

        break;

       }

       else if(buffer[i]=='/'&&buffer[i+]=='/'||buffer[i]==';')

       {

        break;

        //comment

       }

          

         

     i++;

     }

    }

   }

  }

  catch(Exception)

  {

   flag=;

   fclose(file);

   printf("can't open file %s",filename);

   exit();

  }

 fflush(file);

 fclose(file); 

 return flag;

   

}

 

int write_file(char* filename,char* key,char* value)

{

 int flag=;

 FILE* file;

 file=fopen(filename,"a");

 try

 {

  if(file==NULL)

  {

  flag=;

  throw(flag);

  }

  fprintf(file,"$%s=%s\n",key,value);

 }

 catch(Exception)

 { 

  printf("Can't write file %s",filename);

  exit();

 }

 fflush(file);

 fclose(file);

 return flag; 

}

 

int write_section(char* filename,char* section)

{

 int flag=;

 FILE* file=NULL;

 try

 {

  file=fopen(filename,"a");

  if(file!=NULL)

  {

   fprintf(file,"[%s]\n",section);

  }

  else

  {

   int flag=;

   throw(flag);

  }

 }

 catch(Exception)

 {

  printf("can't open file %s",filename);

  exit();

 }

 fflush(file);

 fclose(file);

 return flag; 

}

int release()

{

 int flag=;

 return flag;

}

int read_section(char* filename)

{

 return ;

}

int getAuthor(char* value)

{

 char author[]="武汉软件工程职业学院计算机应用系孟德军";

 int i=;

 for(i=;i<strlen(author);i++)

 {

 value[i]=author[i];

 }

 return ;

}

void getVersion(char* value)

{

 char version[]="////";

 int i=;

 for(i=;i<strlen(version);i++)

 {

 value[i]=version[i];

 }

   

}

/**************************************************************************

void main()

{

 char* str=NULL;

 char author[];

 char buffer[];

 char buffer[];

 char buffer[];

 read_file("F:\\exercise\\C++!C\\sys.ini","password",buffer);

 read_file("F:\\exercise\\C++!C\\sys.ini","username",buffer);

 read_file("F:\\exercise\\C++!C\\sys.ini","driver",buffer);

 printf("password=%s\n",buffer);

 printf("\n");

 printf("username=%s\n",buffer);

 printf("\n");

 printf("driver=%s\n",buffer);

 getAuthor(author);

 printf("\n");

 printf("author=%s",author);

 release();

}
复制代码
分享好友

分享这个小栈给你的朋友们,一起进步吧。

JAVA玩具小屋
创建时间:2019-08-16 16:54:49
分享程序开发方面的小经验,思考一些比较简单易懂的技术问题
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • Yios5092
    栈主

小栈成员

查看更多
  • 栈栈
  • coyan
  • 25minutes
  • ?
戳我,来吐槽~