當前位置:首頁 » 編程語言 » c語言解析文件

c語言解析文件

發布時間: 2023-06-16 03:24:38

⑴ yaml文件用c語言如何解析出鍵值對啊謝謝

有一個C語言的解析包:LibYAML 是一個 C 語言的包,用來解析 YAML 1.1 數據。
當前版本是 LibYAML: 0.1.5 (2014-02-04).

⑵ 用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。
寫到這里,本文的意圖基本達到,如果您可以把某些代碼親自實現,相信讀者會有比較大的收獲。

⑶ 如何用C語言實現解析HTML文檔

參考下面代碼:
#include <stdio.h>
#include <streamhtmlparser/htmlparser.h>
int main(void)
{
unsigned int getchar_ret;
htmlparser_ctx *parser = htmlparser_new();
while ((getchar_ret = getchar()) != EOF) {
char c = (char)getchar_ret;
/* If we received a '$' character, we output the current tag and attribute
* * name to stdout. */
if (c == '$') {
printf("[[ ");
if (htmlparser_tag(parser)) printf("tag=%s ", htmlparser_tag(parser));
if (htmlparser_attr(parser)) printf("attr=%s ", htmlparser_attr(parser));
printf("]]");
/* If we read any other character, we pass it to the parser and echo it to
* * stdout. */
} else {
htmlparser_parse_chr(parser, c);
putchar(c);
}
}
}

⑷ 怎麼用c語言解析二進制文件

//ver:1
//resv1:0
//signature:CUC
//type:69
//no:5
//resv2:0
//seq:4
//length:56
//int2,1
//int2,2
//int1,1
//int4,30
//int1,1
//int1,7
//str,beijing
//int2,1
//int2,4
//int1,1
//int4,60
//int1,1
//int1,7
//str,tianjin
//int4,80
//二進制文件b.bin是:
//00000000h:
//00000010h:
//00000020h:
//00000030h:6E6A696E00000050
#pragmacomment(lib,"ws2_32")
#include<stdio.h>
#include<winsock2.h>
#pragmapack(push,1)
struct_D{
charver;
//charresv1;
charsignature[3];
chartype;
shortno;
charresv2;
intseq;
intlength;
shortint2_0;
shortint2_1;
charint1_0;
intint4_0;
charint1_1;
charint1_2;
charstr_0[7];
shortint2_2;
shortint2_3;
charint1_3;
intint4_1;
charint1_4;
charint1_5;
charstr_1[7];
intint4_2;
}d;
#pragmapack(pop)
FILE*f;
intmain(){
f=fopen("b.bin","rb");
if(NULL==f){
printf("Cannotopenfileb.bin! ");
return1;
}
fread(&d,sizeof(struct_D),1,f);
fclose(f);
printf("ver:%d ",d.ver);
printf("resv1:0 ");
printf("signature:%.3s ",d.signature);
printf("type:%d ",d.type);
printf("no:%hd ",ntohs(d.no));
printf("resv2:%d ",d.resv2);
printf("seq:%d ",ntohl(d.seq));
printf("length:%d ",ntohl(d.length));
printf("int2_0:%hd ",ntohs(d.int2_0));
printf("int2_1:%hd ",ntohs(d.int2_1));
printf("int1_0:%d ",d.int1_0);
printf("int4_0:%d ",ntohl(d.int4_0));
printf("int1_1:%d ",d.int1_1);
printf("int1_2:%d ",d.int1_2);
printf("str_0:%.7s ",d.str_0);
printf("int2_2:%hd ",ntohs(d.int2_2));
printf("int2_3:%hd ",ntohs(d.int2_3));
printf("int1_3:%d ",d.int1_3);
printf("int4_1:%d ",ntohl(d.int4_1));
printf("int1_4:%d ",d.int1_4);
printf("int1_5:%d ",d.int1_5);
printf("str_1:%.7s ",d.str_1);
printf("int4_2:%d ",ntohl(d.int4_2));
return0;
}
//ver:1
//resv1:0
//signature:CUC
//type:69
//no:5
//resv2:0
//seq:4
//length:56
//int2_0:1
//int2_1:2
//int1_0:1
//int4_0:30
//int1_1:1
//int1_2:7
//str_0:beijing
//int2_2:1
//int2_3:4
//int1_3:1
//int4_1:60
//int1_4:1
//int1_5:7
//str_1:tianjin
//int4_2:80
//

代碼示例

⑸ 怎麼用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文件就行了

熱點內容
銳志哪個配置性價比最高 發布:2025-02-12 17:38:43 瀏覽:917
智能推送演算法 發布:2025-02-12 17:38:41 瀏覽:834
拍照上傳器 發布:2025-02-12 17:34:29 瀏覽:651
androidweb框架 發布:2025-02-12 17:32:45 瀏覽:75
安卓編程賀卡 發布:2025-02-12 17:32:44 瀏覽:837
php獲取資料庫的欄位 發布:2025-02-12 17:29:02 瀏覽:765
伺服器地址消失 發布:2025-02-12 17:23:36 瀏覽:950
後台執行php腳本 發布:2025-02-12 17:21:45 瀏覽:470
spring編程式事務 發布:2025-02-12 17:16:55 瀏覽:397
nginx禁止ip訪問 發布:2025-02-12 17:15:14 瀏覽:273