第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

用C語言讀取xml文件,怎么實(shí)現(xiàn)?

用C語言讀取xml文件,怎么實(shí)現(xiàn)?

C++ C
尚方寶劍之說 2019-05-24 19:15:04
用C語言讀取xml文件,怎么實(shí)現(xiàn)?
查看完整描述

3 回答

?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

我上次才給人寫過 

xml文件內(nèi)容

 

<?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>

 

運(yùn)行結(jié)果

 

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);

}


查看完整回答
反對 回復(fù) 2019-05-26
?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

xml文件和txt文件相同,使用普通的文本操作函數(shù)即可讀取。

1、C語言標(biāo)準(zhǔn)庫提供了一系列文件操作函數(shù)。文件操作函數(shù)一般以f+單詞的形式來命名(f是file的簡寫),其聲明位于stdio.h頭文件當(dāng)中。例如:fopen、fclose函數(shù)用于文件打開與關(guān)閉;fscanf、fgets函數(shù)用于文件讀??;fprintf、fputs函數(shù)用于文件寫入;ftell、fseek函數(shù)用于文件操作位置的獲取與設(shè)置。
2、例程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include<stdio.h>

int a;

char b,c[100];

int main(){

    FILE * fp1 = fopen("input.xml", "r");//打開xml格式輸入文件

    FILE * fp2 = fopen("output.txt", "w");//打開輸出文件

    if (fp1==NULL || fp2==NULL) {//若打開文件失敗則退出

        puts("不能打開文件!");

        rturn 0;

    }

    fscanf(fp1,"%d",&a);//從輸入文件讀取一個(gè)整數(shù)

    b=fgetc(fp1);//從輸入文件讀取一個(gè)字符

    fgets(c,100,fp1);//從輸入文件讀取一行字符串

     

    printf("%ld",ftell(fp1));//輸出fp1指針當(dāng)前位置相對于文件首的偏移字節(jié)數(shù)

     

    fputs(c,fp2);//向輸出文件寫入一行字符串

    fputc(b,fp2);//向輸出文件寫入一個(gè)字符

    fprintf(fp2,"%d",a);//向輸出文件寫入一個(gè)整數(shù)

     

    fclose(fp1);//關(guān)閉輸入文件

    fclose(fp2);//關(guān)閉輸出文件,相當(dāng)于保存

    return 0;

}


查看完整回答
反對 回復(fù) 2019-05-26
  • 3 回答
  • 0 關(guān)注
  • 2024 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號