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模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
随机推荐
- [Erlang34]erlang.mk的源码阅读1-入门makefile
通过erlang.mk项目,掌握基本的makefile语法,可以自己定制makefile. 1. makefile 基本规则: 1. 所有的源文件没有被编译过,则对各个源文件进行编译并进行链接,生成最 ...
- 【QT】二进制读取图像文件并显示
打开对话框选择文件 二进制方式读取文件 转换成图像显示 void MainWindow::showImage() { //打开文件对话框 QString lastPath="D:/Engli ...
- python+pcap+dpkt抓包小实例
通过pcap与dpkt抓包解包示例: #!/usr/bin/env python # -*- coding: utf-8 -*- """ 网络数据包捕获与分析程序 &qu ...
- 配置AndroidStdio的开发环境
http://blog.csdn.net/siwuxie095/article/details/53431818
- 【OCP|052】OCP 11g最新考题收集整理-第6题
6.You are installing Oracle Grid Infrastructure by using the Oracle Universal Installer (OUI). You s ...
- Mysql Insert Or Update语法例子
有的时候会需要写一段insert的sql,如果主键存在,则update:如果主键不存在,则insert.Mysql中提供了这样的用法:ON DUPLICATE KEY UPDATE.下面就看看它是如何 ...
- linux中rc.d目录下的文件
参考 http://blog.sina.com.cn/s/blog_414d78870102vqj5.html http://www.360doc.com/content/12/0820/17/933 ...
- 为什么说 Gumroad 是一家 “失败” 的创业公司?
Gumroad 是一家 "失败" 的创业公司. 创立于 2012 年,Gumroad 是一个面向创造者的电商平台.创始人 Sahil Lavingia,一名 19 岁的少年,Pin ...
- SQL面试题(网络收集)
1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三 语文 81 张三 数学 75 李四 ...
- java_对象序列化
对象序列化(serializable) 序列化读:ObjectInputStream ois=new ObjectInputStream(new FileInputStream("./gg ...