本文实例讲述了C语言读写配置文件的方法。分享给大家供大家参考。具体如下:
CException.h如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/************************************************************************/
/* 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如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#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语言程序设计有所帮助。