当前位置:首页 » 编程语言 » c语言解析配置文件
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言解析配置文件

发布时间: 2022-05-08 17:52:15

1. c语言读写配置文件

#include <stdio.h>
#include <string.h>

#define MAX_BUF 20
#define SERVER "localhost"
#define CONFIG_FILE "1.conf"

bool SetAuthServer(char* strServerAdd)
{
char buf[MAX_BUF], tempBuf[MAX_BUF];
memset(buf, 0, MAX_BUF);
memset(tempBuf, 0, MAX_BUF);
FILE *pF = fopen(CONFIG_FILE, "r");
if(!pF)
{
printf("打开文件失败!\n");
return false;
}
fread(buf, MAX_BUF, 1, pF);
if(!feof(pF))
{
printf("读取不完整,请把MAX_BUF设置为大一点, 当前大小为: %d\n", MAX_BUF);
fclose(pF);
return false;
}
fclose(pF);
char *lpPos = buf;
char *lpNewPos = buf;
while(lpNewPos = strstr(lpPos, SERVER))
{
strncpy(tempBuf+strlen(tempBuf), lpPos, lpNewPos-lpPos);
strcat(tempBuf, strServerAdd);
lpPos = lpNewPos + strlen(SERVER);
}

strcat(tempBuf, lpPos);
pF = fopen(CONFIG_FILE, "w");
if(!pF)
{
printf("打开文件失败!\n");
return false;
}
fwrite(tempBuf, strlen(tempBuf), 1, pF);
fclose(pF);
return true;
}

void main()
{
char buf[20];

printf("请输入一个字符串来修改服务器配置: ");
scanf("%s", buf);
if(SetAuthServer(buf) == true)
printf("修改成功!\n");
else
printf("修改失败!\n");

}

2. c语言如何解析xml并将所有内容存入数组

/*前段时间恰好做过类似的东西,代码可以给你参考下。
*Xml配置见最后
*/

typedefstructSrcFileFmt
{
intColID;
charColCode[64];/*字段英文名称*/
charColName[128];/*字段中文名称*/
charColType[20];/*字段类型(包含长度)*/
charColComment[128];/*字段描述*/
}SrcFileFmt;

intmain(intargc,char**argv)
{
SrcFileFmtSrcFileFmt[128];
intiNum=-1;
if(2>argc)
{
printf("Usage:%sSrcXmlFile ",argv[0]);
return-1;
}
iNum=parseSourceCfg(SrcCfgFile,SrcFileFmt);
if(iNum==-1)
{
return-1;
}
return0;
}

/*调用此函数后,xml文件的内容会被存储到结构体数组SrcFileFmtsrcfilefmt[]中
*此函数依赖于libxml2-2.9.2.tar.xz
*/
intparseSourceCfg(char*FileName,SrcFileFmtsrcfilefmt[])
{/*解析源文件xml,FileName为源xml文件名*/
xmlDocPtrdoc;
xmlNodePtrcur,root;
charsFileName[64]={''};
intcnt=0;
if(FileName==NULL)
{
return-1;
}
sprintf(sFileName,"%s.xml",FileName);
doc=xmlParseFile(sFileName);
if(doc==NULL)
{
return-1;
}
root=xmlDocGetRootElement(doc);
if(root==NULL){
xmlFreeDoc(doc);
return(-1);
}
if(xmlStrcmp(root->name,(constxmlChar*)"SrcRoot"))
{
xmlFreeDoc(doc);
return-1;
}

cur=root->xmlChildrenNode;
while(cur!=NULL)
{
if((!xmlStrcmp(cur->name,(constxmlChar*)"Column")))
{
xmlChar*key;
xmlNodePtrcur_sub=cur;
cur_sub=cur_sub->xmlChildrenNode;

while(cur_sub!=NULL)
{
if((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColID"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
srcfilefmt[cnt].ColID=atoi((char*)key);
xmlFree(key);
}
if((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColCode"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColCode,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColName"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColName,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColType"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColType,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColComment"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColComment,(char*)key);
xmlFree(key);
}
cur_sub=cur_sub->next;
}
cnt++;
}
cur=cur->next;
}
xmlFreeDoc(doc);
returncnt;
}

<SrcRoot>
<Column>
<ColID>1</ColID>
<ColCode>kmh</ColCode>
<ColName>字段1</ColName>
<ColType>VARCHAR(11)</ColType>
</Column>
<Column>
<ColID>2</ColID>
<ColCode>dfkmh</ColCode>
<ColName>字段2</ColName>
<ColType>VARCHAR(11)</ColType>
</Column>
<Column>
<ColID>3</ColID>
<ColCode>hbh</ColCode>
<ColName>字段3</ColName>
<ColType>INTEGER(10)</ColType>
</Column>
</SrcRoot>

3. c语言怎么加载自己写的配置文件

这个问题和c语言关系不是太大。保存和加载配置的方式有很多,看你的平台处理能力以及配置复杂度了,如果是很复杂的情况,可以使用sqlite数据库;一般也可以用json/xml/ini等通用格式;简单点可以直接用文本文件,或者二进制文件保存自定义格式。
前面两种方式可以用相关的库处理,后面这种就自己写了,方便调试的话,最好是用文本方式保存,这样比较容易检查。最直接的方式就是用类似KEY=VALUE这样的配对,比如:username=abc。每一个配置之间的分隔符可以直接用回车,这样方便查看最终保存的结果。操作的话,基本上就是定义一个配置的数据结构,然后填入所有配置数据,再挨个写进文件。读取的时候,就按KEY来解析VALUE,然后填入相应的位置。

4. 用C语言读取一个文件中的内容,如何对不同的行进行解析,比如是配置文件

很简单的

配置文件 微软有抓们的一套解析函数

INI文件是Windows系统中一类比较重要的文件,通常用来存放系统或者应用程序的配置信息,以方便系统或者应用 程序在初始化时再次读入。比如Windows系统中的配置文件win.ini和system.ini,它们就主要存放系统启动或用户登陆时的系统信息。这 项功能在方便了系统配置的同时,也为非法程序的自动运行提供了可乘之机。显然,这类文件的重要性应该引起我们的重视。但是对于这样的ini文件的读写操作 却与普通文本文件有着种种的不同,尤其体现在编程实现上。笔者曾经尝试用手动更改的方法在文件中加入一些项,使得自己的程序能够在初始化时自动运行,但是 却没有成功,最后还是借由编程的方法来实现了。这里主要涉及到一些API函数,而这些函数又往往不被人们所熟知,本文的任务就是在介绍这些函数的同时,用 简单的程序作了示例,下面我们言归正传。
先来看几个往配置文件中写入信息的函数:
(1)WritePrivateProfileSection()用来在ini文件中直接向指定区域写入键和值的信息,其原型如下:
BOOL WritePrivateProfileSection(
LPCTSTR lpAppName, // 指向指定字段的字符串
LPCTSTR lpString, // 指向要写入的键与值字符串
LPCTSTR lpFileName // 指向文件名称字符串,如果不包含完整路径,则在windows目录下创建
);
用法示例:
WritePrivateProfileSection(_T(“windows”),_T(“load=c:\\winnt\\notepad.exe”),_T(“c:\\winnt\\win.ini”));
(2)WritePrivateProfileString()与上一个函数的不同点在于其将键和值分开了,原型如下:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 指向指定字段的字符串
LPCTSTR lpKeyName, // 指向指定键的字符串
LPCTSTR lpString, // 指向指定值的字符串
LPCTSTR lpFileName // 指向文件名称字符串
);
用法示例:
WritePrivateProfileString(_T(“windows”),_T(load”)_T(“c:\\winnt\\notepad.exe”),_T(“c:\\winnt\\win.ini”));
(3)WritePrivateProfileStruct()与前面两个的不同在于文件尾有校验和,原型如下:
BOOL WritePrivateProfileStruct(
LPCTSTR lpszSection, //指向指定字段的字符串
LPCTSTR lpszKey, //指向指定键的字符串
LPVOID lpStruct, //指向存放要加入的数据的缓冲区,如果为NULL,则删除键
UINT uSizeStruct, //缓冲区大小,以字节为单位
LPCTSTR szFile //以零结尾的文件名称字符串,如果为空,则向win.ini写入
);
用法示例:
WritePrivateProfileStruct(_T(“windows”),_T(“load”),pBuffer,sizeof(pBuffer),_T(“c:\\winnt\\win.ini”));
(4)还有两个函数,是专门用来向win.ini文件写入的,函数原型如下:
BOOL WriteProfileSection(
LPCTSTR lpAppName, //指向指定字段的字符串
LPCTSTR lpString //指向指定值的字符串
);
BOOL WriteProfileString(
LPCTSTR lpAppName, //指向指定字段的字符串
LPCTSTR lpKeyName, //指向指定键的字符串
LPCTSTR lpString //指向指定值的字符串
);
下面来看几个对应的从ini文件获取信息的API函数,上面已经说得很详细了,这里只说其中两个:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, //指向指定字段的字符串
LPCTSTR lpKeyName, //指向键的字符串
LPCTSTR lpDefault, //如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量
LPTSTR lpReturnedString, //存放INI文件中值的目的缓存
DWORD nSize, //目的缓冲区的大小,以字节为单位
LPCTSTR lpFileName //指向INI文件名称的字符串
);

UINT GetPrivateProfileInt(
LPCTSTR lpAppName, //指向指定字段的字符串
LPCTSTR lpKeyName, //指向键的字符串
INT nDefault, //如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量
LPCTSTR lpFileName //指向INI文件名称的字符串
);
程序示例1: 我们在这里建立了一个应用程序“App Name”,并且使用了一个INI文件“appname.ini”,在此INI文件中,我们写入如下内容:
[Section1]
FirstKey = It all worked out okay.
SecondKey = By golly, it works.
ThirdKey = Another test.
代码分析如下:
#include <stdio.h>
#include <windows.h>
//主函数
main()
{
//定义局部
CHAR inBuf[80];
HKEY hKey1, hKey2;
DWORD dwDisposition;
LONG lRetCode;
// 试图创建INI文件的键值
lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT
\\CurrentVersion\\IniFileMapping\\appname.ini",
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
NULL, &hKey1,
&dwDisposition);
//判断是否出错
if (lRetCode != ERROR_SUCCESS){
printf ("Error in creating appname.ini key\n");
return (0) ;
}
//试图设置一个节区的值
lRetCode = RegSetValueEx ( hKey1,
"Section1",
0,
REG_SZ,
"USR:App Name\\Section1",
20);
//判断是否出错
if (lRetCode != ERROR_SUCCESS) {
printf ( "Error in setting Section1 value\n");
return (0) ;
}
//试图创建一个应用名称键值
lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER,
"App Name",
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
NULL, &hKey2,
&dwDisposition);

//判断是否出错
if (lRetCode != ERROR_SUCCESS) {
printf ("Error in creating App Name key\n");
return (0) ;
}
//强制系统重新读取映射区的内容到共享内存中,以便于将来对应用程序的调用可//以找到它,而不需要重新启动系统
WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" );
//向INI文件中添加一些键值
WritePrivateProfileString ("Section1", "FirstKey",
"It all worked out okay.", "appname.ini");
WritePrivateProfileString ("Section1", "SecondKey",
"By golly, it works.", "appname.ini");
WritePrivateProfileSection ("Section1", "ThirdKey = Another Test.",
"appname.ini");
//测试一下添加的正确性
GetPrivateProfileString ("Section1", "FirstKey",
"Bogus Value: Get didn't work", inBuf, 80,
"appname.ini");
printf ("%s", inBuf);
return(0);
}

程序示例2:通过修改win.ini中的字段[windows]中的键load或run,或者是为system.ini中的字段[boot]中的键 shell增加值,可以达到设置程序自动运行的目的。假设我们要自动运行notepad.exe,修改后的win.ini或system.ini文件象这 样就可以:
win.ini
[windows]
load=c:\winnt\notepad.exe
run=c:\winnt\notepad.exe

system.ini
[boot]
shell=c:\winnt\explorer.exe c:\winnt\notepad.exe
注意:system.ini文件的修改要特别注意,如果你单纯改成shell=c:\winnt\notepad.exe,则不能首先运行 explorer.exe,很明显你将看不到桌面和任务栏,呵呵,笔者在做实验时就曾因为粗心造成了这样的后果,不过不用害怕,只要你用我们下面提供的程 序,将它修改过来就可以了,默认时,系统在system.ini中的[boot]下是shell=c:\winnt\explorer.exe。很多非法 程序就是通过修改这两个文件来达到自启动的目的的。
下面这个程序可以在附书光盘中找到,名称为“AutoPlay”,使用VC++6.0写成,核心程序源代码如下:
void CAutoRunDlg::OnBrowse()
{
//只浏览exe文件
CfileDialog fileDlg(TRUE,_T("EXE"),_T("*.exe"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(_T("Executable Files (*.exe) |*.exe ||")));//显示打开文件的对话框

//当操作者选择OK时,程序取得选择文件的全路径名(包括文件的路径及文件名称),并将相应的数值传输给相关的控件变量。
if(fileDlg.DoModal()==IDOK)
{
m_strFileName=fileDlg.GetPathName();

//向将变量中的数值传输给控件显示出来。
UpdateData(FALSE);
}
}
void CAutoRunDlg::OnApply()
{
//更新数据
UpdateData(TRUE);
//写入ini文件
LPCTSTR filename;
filename=m_strFileName;
WritePrivateProfileString(_T("windows"),_T("load"),filename,_T("c:\\winnt\\win.ini"));
}

您如果要更改system.ini,可以将WritePrivateProfileString(_T("windows"),_T("load"),filename,_T("c:\\winnt\\win.ini"));
改为 WritePrivateProfileString(_T("boot"),_T("shell"),filename,_T("c:\\winnt \\system.ini"));并且在输入文件名时输入c:\winnt\explorer.exe c:\winnt\notepad.exe。
写到这里,本文的意图基本达到,如果您可以把某些代码亲自实现,相信读者会有比较大的收获。

5. Ubuntu如何用C语言写从系统配置文件中读取ip和端口的相关代码

可以写成命令行参数的模式的,不用修改代码,形如:
./server192.168.1.11210088
./client192.168.1.11210088

6. C语言读写大量配置文件有什么好的建议

读写配置文件在linux下的格式和windows下不太相同,附件是一些学习资料,后续会慢慢更新

linux系统下的配置文件

XDG_DESKTOP_DIR="$HOME/桌面"
XDG_DOWNLOAD_DIR="$HOME/下载"
XDG_TEMPLATES_DIR="$HOME/模板"
XDG_PUBLICSHARE_DIR="$HOME/公共的"
XDG_DOCUMENTS_DIR="$HOME/文档"
XDG_MUSIC_DIR="$HOME/音乐"
XDG_PICTURES_DIR="$HOME/图片"
XDG_VIDEOS_DIR="$HOME/视频"molea]

windows系统下的配置文件

[molea]

name1=value1
name2=value2

[moleb]
name3=value3
name4=value4

一般嵌入式上用的都是linux这种风格

7. 标准C语言,修改配置文件

在1.1后面带空格
只要该行数据的最大长度确定,在你这行数据确定后,不足部分全部用空格覆盖。
比如ver=1.0.2现在的长度是9个字符,如果我确定这行内容不会超过12个字符,那后面的内容我可以全用空格覆盖,最后只要加个回车即可。

不知道你所谓的以后改不方便指什么

8. c语言读配置文件 xxx=2 怎么把这个2读出来存到相关变量里

fscanf(fp,"%c%c%c%c%d",x1,x2,x3,x4,x5);
先找到你需要的这行,直接读。把不需要的剔除。注意字符。

9. 怎么使用C语言读取properties配置文件

用C语言读取properties配置文件的方法:
1、找到配置路径下的properties文件
2、按行读取文件内容
具体实现代码如下:
//定义读入的行数组,1024行
char
line[1024];
//存放配置项数组setting
int
setting[N],i
=
0;
//开始循环读入
while(fgets(fp,line,1024)
!=
NULL)
{
//读入配置的值给line变量
fscanf(line,"setting%*d
=
%d",&setting[i++]);
}

10. 怎么用c语言解析xml文件

我上次才给人写过
xml文件内容

<?xml version="1.0" encoding="UTF-8" ?>
- <aicomoa_response>
- <country_list>
- <country>
<id>7</id>
<pid>0</pid>
<continent_id>1</continent_id>
<guohao>93</guohao>
<cntitle>阿富汗</cntitle>
<entitle>Afghanistan</entitle>
<hztitle>阿富汗</hztitle>
<jptitle>アフガニスタン</jptitle>
<kotitle>??????</kotitle>
<jp_pinyin>ア</jp_pinyin>
<pinyin>AFuHan</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
- <country>
<id>8</id>
<pid>0</pid>
<continent_id>2</continent_id>
<guohao>355</guohao>
<cntitle>阿尔巴尼亚</cntitle>
<entitle>Albania</entitle>
<hztitle>阿尔巴尼亚</hztitle>
<jptitle>アルバニア</jptitle>
<kotitle />
<jp_pinyin>ア</jp_pinyin>
<pinyin>AErBaNiYa</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
</country_list>
</aicomoa_response>

运行结果

Info[0]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Info[1]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Press any key to continue

代码

#include <stdio.h>
#include <string.h>
main()
{
int i=0;
FILE *fp;
char szFileBuff[1024] = {0}, szBuff[100][1024];
char id[10] = {0}, pid[10] = {0}, continent_id[10] = {0}, guohao[10] = {0},
cntitle[64]= {0},entitle[64]= {0},hztitle[64] = {0},jptitle[64] = {0},
kotitle[64] = {0},jp_pinyin[64] = {0}, pinyin[64] = {0},sid[10] = {0},jibie[10] = {0};
char *lFirst, *lEnd;

fp = fopen("country.txt","r");
if (fp==NULL)
{
printf("read XML file error!\n");
}
while(fgets(szFileBuff, 1023, fp))
{
if ((lFirst = strstr(szFileBuff, "<id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</id>");
memcpy(id, lFirst + 4, lEnd - lFirst - 4);
}
if ((lFirst = strstr(szFileBuff, "<pid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pid>");
memcpy(pid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<continent_id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</continent_id>");
memcpy(continent_id, lFirst + 14, lEnd - lFirst - 14);
}
if ((lFirst = strstr(szFileBuff, "<guohao>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</guohao>");
memcpy(guohao, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<cntitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</cntitle>");
memcpy(cntitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<entitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</entitle>");
memcpy(entitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<hztitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</hztitle>");
memcpy(hztitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jptitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jptitle>");
memcpy(jptitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<kotitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</kotitle>");
memcpy(kotitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jp_pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jp_pinyin>");
memcpy(jp_pinyin, lFirst + 11, lEnd - lFirst - 11);
}
if ((lFirst = strstr(szFileBuff, "<pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pinyin>");
memcpy(pinyin, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<sid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</sid>");
memcpy(sid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<jibie>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jibie>");
memcpy(jibie, lFirst + 7, lEnd - lFirst - 7);
}
if ((lFirst = strstr(szFileBuff, "</country>")) != NULL)
{
sprintf(szBuff[i],"id:%s|pid:%s|continent_id:%s|guohao:%s|cntitle:%s|entitle:%s|hztitle:%s|jptitle:%s|kotitle:%s|jp_pinyin:%s|pinyin:%s|sid:%s|jibie:%s|",
id,pid,continent_id,guohao,cntitle,entitle,hztitle,jptitle,kotitle,jp_pinyin, pinyin,sid,jibie);
printf("Info[%d]=[%s]\n",i++, szBuff);
}
}
fclose(fp);
}

补充:你这个就说得太笼统了,
1 你上传的xml文件具体格式是什么?
2 要在网页上显示的具体格式是什么
3 你根本不知道怎么做 所以也不知道怎么问
我不用关心你的c语言的cgi吧?我才不管是用什么上传的
只有你说的嵌入式三个字 给我一点有用信息 就是解析这个xml用插件恐怕是不行
只能C语言
4 我现在只要求你的xml文件格式和 网页上要显示哪些xml中解析出来的信息

只要知道这些 我只需要在我的程序上加上生成html文件就行了