Это довольно долго, так со мной ...
Хорошо, вот моя цель: прочитать текстовый файл XML и разбить каждое слово и тег на свою строку в массиве.
Например, если я введу этот текст в свою программу:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Я получу это:
<note>
<to>
Tove
</to>
<from>
...
Прямо сейчас у меня есть код, который может успешно выполнитьэто, но только со словами, поэтому вместо приведенного выше списка я получаю:
note
to
Tove
...
Я хочу сохранить теги, или я не смогу делать то, что я хочу с ним.Итак, я пытался заставить его также добавлять теги, но мне не удавалось
Хорошо, вот мой код:
//While the file is not empty
while(fgets(buffer, sizeof(buffer), stdin) != NULL){
int first = 0;
int last = 0;
//While words are left in line
while(last < INITIAL_SIZE && buffer[last] != '\0'){
int bool = 0;
//Tag detected
if(buffer[last] == '<'){
while(buffer[last] != '>'){
last++;
}
bool = 1;
}else{
//While more chars are in the word
while(last < INITIAL_SIZE && isalpha(buffer[last])){
last++;
}
}
//Word detected
if(first < last){
//Words array is full, add more space
if(numOfWords == sizeOfWords){
sizeOfWords = sizeOfWords + 10;
words = (char **) realloc(words, sizeOfWords*sizeof(char *));
}
//Allocate memory for array
words[numOfWords] = (char *) calloc(last-first+1, sizeof(char));
for(i = 0; i < (last-first); i++){
words[numOfWords][i] = buffer[first + i];
}
//Add terminator to "new word"
words[numOfWords][i] = '\0';
numOfWords++;
}
//Move "Array Pointers" accordingly
last++;
first = last;
}
}
У любого есть идеи, с приведенным выше кодомэто распечатка:
<note
<to
Tove
to
<from
Jani
from
<heading
...
Don
t
forget
me
this
weekend
</body
</note
Итак, после этой стены текста кто-нибудь имеет представление о том, как я могу изменить свой текущий код, чтобы заставить его работать?Или у кого-нибудь еще есть альтернатива?Любые предложения или помощь приветствуется.