C语言读写配置文件--转载
http://www.oschina.net/code/snippet_4873_2503
[].[代码] 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=; 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(); }
C语言读写配置文件--转载的更多相关文章
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- R语言读写中文编码方式
最近遇到一个很头疼的事,就是 R语言读写中文编码方式.在网上找到了一篇博文,谢谢博主的精彩分享,让我很快解决了问题,在此也分享一下 R语言读写数据的方法很多,这里主要是我在使用read.csv/rea ...
- Python-通过configparser读写配置文件
Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...
- 基于Python的接口自动化实战-基础篇之读写配置文件
引言 在编写接口自动化测试脚本时,有时我们需要在代码中定义变量并给变量固定的赋值.为了统一管理和操作这些固定的变量,咱们一般会将这些固定的变量以一定规则配置到指定的配置文件中,后续需要用到这些变量和变 ...
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
- 使用ConfigurationManager类读写配置文件
使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboard print? using System; usi ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- C读写配置文件
在项目开发中,经常需要读取应用配置文件的初始化参数,用于应用在启动前进行一些初始化配置.比如:Eclipse,参数项包含主题.字体大小.颜色.Jdk安装位置.自动提示等.Eclispe配置的文件格式是 ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
随机推荐
- 在JS中简单实现Formatter函数
JS原生并没有提供方便使用的Formatter函数,用字符拼接的方式看起来混乱难读,而且使用起来很不方便.个人感觉C#里提供的语法比较好用,如: String.Format("Welcome ...
- CodeIgniter使用中写的一些文章
CI的captcha替代类库: http://www.ifixedbug.com/posts/codeigniter-captcha-library 原生的captcha不是太好用,自己组装一个吧. ...
- roadflow asp.net工作流自定义表单
在roadflow表单设计器不能满足很复杂的业务需求的时候,可以采用自定义表单(即表单页面自己做). 自定义表单就是自己写一个页面,包含控制器视图,然后将这个页面挂到流程上进行审批. 自定义表单分为以 ...
- 开源应用框架BitAdminCore:更新日志20180903
索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:https://www.bitadmincore.com 框架源码:https://github.com/chenyi ...
- Delphi中MessageBox用法
消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.最简单用法,不带图形 MessageBox(0,'不同意','提示',MB_OK); MessageBo ...
- 内置函数enumerate()使用
描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. Python 2.3. 以上版本可用,2. ...
- 超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群
超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群 ps:本文的步骤已自实现过一遍,在正文部分避开了旧版教程在新版使用导致出错的内容,因此版本一致的情况下照搬执行基本不会有大错误. ...
- 动态代理(CGLIB实现)
CGLIB(Code Generation Library)是一个开源项目.可以直接对类进行增强,而不需要像JDK的动态代理,需要增强的类必须实现某接口 在使用Spring框架时,因为Spring框架 ...
- C语言中宏定义与C++中的内联函数
一,宏定义:在预处理的时候把宏定义的内容替换到代码中,正常编译. 1,无参数宏定义和有参数宏定义 (1)宏定义不能加分号,比如:#define PI 3.24;错的,#define PI 3.24 ...
- jenkins+gitlab+robot framework 中遇到的坑
问题一:拉Git源代码时提示无权限 原来之前用的ssh密钥一直都是自己的用户生成的.其实在Jenkins系统使用的都是Jenkins这个系统帐号的. 解决方法: 切换到jenkins这个帐号下生成个新 ...