Problème avec fgetpos / fsetpos
Bonjour,
Je suis en train d'écrire un bout de code qui permet de charger un dictionnaire de mot/traduction dans une structure c++ map.
Le dictionnaire est lu depuis un fichier texte et a la structure suivante :
mot:traduction/mot2:traduction2/mot3:traduction3/...motN:traductionN
Voici la fonction développée :
Code:
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
|
map<string,string> dictionary;
void loadDictionary (string dicName) {
FILE* fDic = fopen(("/path/"+dicName+".txt").c_str(), "r");
if (fDic == NULL) LOGE("ERROR opening dic file");
char* buffer = 0;
buffer = (char*) malloc (100 * sizeof(char)); // precondition = word+translation < 100 char
if (buffer == 0) LOGE("ERROR malloc buffer dic file");
fpos_t readPos;
/*
* initialisation :
* - readPos = beginning of fDic
* - entryCount = 0
* - endOfFile = false
* invariant :
* - readPos is always positionned after a word end
* - entryCount is the number of word added to the dictionary
* - endOfFile is true only if there is no more characters to read
* progression :
* - readPos = position after the last read word
* - entryCount += number of words added to the dictionary during this loop iteration
* - endOfFile = (is fDic totaly read ?)
* stop condition :
* - endOfFile = true
*/
fgetpos(fDic, &readPos);
int entryCount = 0;
int currentPosition = 0;
bool endOfFile = false;
while (!endOfFile) {
int i;
bool wordOk;
string word, translation;
if (fsetpos(fDic, &readPos) != 0) LOGE("Error fsetpos");
int nbRead = fread (buffer, 1, 100, fDic);
if (nbRead < 100) {
endOfFile = true;
LOGE("end of file");
}
wordOk = false;
for (i=0; i<nbRead; i++) {
if (buffer[i] == ':') {
wordOk = true;
} else if (buffer[i] == '/') {
dictionary.insert(pair<string,string>(word,translation));
LOGE (("new dic entry : "+word+" / "+translation).c_str());
word.erase();
translation.erase();
wordOk = false;
if (fgetpos(fDic, &readPos) != 0) LOGE("Error fgetpos");
entryCount++;
} else {
if (!wordOk) {
word += buffer[i];
} else {
translation += buffer[i];
}
}
}
LOGE(("first char="+char2string(buffer[0])+" // last char="+char2string(buffer[nbRead-1])).c_str());
}
LOGE(("nb entries = "+int2string(entryCount)).c_str());
fclose(fDic);
} |
L'idée est de lire un buffer de 100 caractères, d'ajouter tous les mots+trad complets au dictionnaire et de positionner le curseur à la fin du dernier mot ajouté (c'est à dire après le '/') afin de relire un éventuel dernier mot incomplet.
Exemple :
Code:
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
| Dictionnaire :
ADN:DNA/Abkhasie:Abkhazia/Abkhasien:Abkhazian/Abyssin:Abyssinian/Abyssinie:Abyssinia/Achille:Achilles/Achéron:Acheron/Adam:Adam/Aden:Aden/Adjarie:Adzharia/Adonis:Adonis/Afghan:Afghan/Afghane:Afghan woman/Afghanistan:Afghanistan/Africain:African/Afrikander:Afrikaner/
Résultat attendu :
ADN / DNA
Abkhasie / Abkhazia
Abkhasien / Abkhazian
Abyssin / Abyssinian
Abyssinie / Abyssinia
first char=A // last char=e (dernier e de Achilles, incomplet alors on met la position au premier A de Achille)
Achille/Achilles
Achéron / Acheron
Adam / Adam
Aden / Aden
Adjarie / Adzharia
Adonis / Adonis
Afghan / Afghan
first char=A // last char=A (de Afghane)
Résultat obtenu :
ADN / DNA
Abkhasie / Abkhazia
Abkhasien / Abkhazian
Abyssin / Abyssinian
Abyssinie / Abyssinia
first char=A // last char=e
new dic entry : s / (dernier s de Achilles)
Achéron / Acheron
Adam / Adam
Aden / Aden
Adjarie / Adzharia
Adonis / Adonis
Afghan / Afghan
first char=s // last char=w |
Tout ce passe en fait comme si la position n'était pas modifiée par fsetpos puisque que les tronçons de 100 caractères lus sont toujours contigus. Pourtant je n'obtiens pas d'erreur lors de l'execution de fgetpos et fsetpos.
Merci de m'aider si vous avez une idée du problème !! :ccool: