I'm trying to recover a .txt file text to turn it into a record to create a basic "gps". I did manage to turn the text into a record, however, the longer the code run, the more frequents errors appear (like for exemple it write "hungrygry" instead of "hungry").
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef int bool;
#define TRUE 1
#define FALSE 0
// definir string
typedef char string[1024];
//Define event
typedef struct{
string action[49],preconds[24],add[24],delete[24];
int NbPreconds,NbAdd,NbDelete;
}Event;
int parseLine(char source[], string cible[]){
int i=0, n=0; // i: source[], n: cible[]
while(source[i]!=':') i++; // go until ':'
i++; // avancer au debut de la premiere chaine
int j=i; // point beginning of first chain with j
while(source[i]!='\n'){
if(source[i]==','){
memcpy(&cible[n], &source[j], i-j); // extract characters from j to i
n++;
j=i+1; // j beginning of chain
}
i++;
}
return n;
}
int main(){
Event event[49];
string start[49],finish[49];
int NbStart,NbFinish,i,k,x,Z;
char source[100];
FILE* txtfile = fopen("school.txt","r"); // insérer "monkeys.txt" / "blocs.txt" / "school.txt" pour choisir le fichier
if(txtfile == NULL){ // tester le fichier s'il existe
printf("Fichier inexistant.\n");
exit(0);
}
else{
printf("Fichier ouvert.\n");
}
//Lire les Conditions de départ & de Fin
fgets(source,100,txtfile);
NbStart=parseLine(source,start);
printf("Condition Départ:\n");
for(i=0; i<NbStart;i++){
printf(" - %s\n",start[i]);
}
fgets(source,100,txtfile);
string tempo[]={""};
NbFinish=parseLine(source,finish);
printf("Condition Fin:\n");
for(i=0;i<NbFinish;i++){
printf(" - %s\n",finish[i]);
}
//read action
int NbE=0;
while(fgets(source,100,txtfile)!=NULL){
fgets(source,100,txtfile);
parseLine(source, event[NbE].action);
//read preconds
fgets(source,100,txtfile);
event[NbE].NbPreconds=parseLine(source, event[NbE].preconds);
for(i=0;i<event[NbE].NbPreconds;i++){
printf("Precond: %s\n",event[NbE].preconds[i]);
}
//read add
fgets(source,100,txtfile);
event[NbE].NbAdd=parseLine(source, event[NbE].add);
for(i=0;i<event[NbE].NbAdd;i++){
printf("Add: %s\n",event[NbE].add[i]);
}
//read delete
fgets(source,100,txtfile);
event[NbE].NbDelete=parseLine(source, event[NbE].delete);
for(i=0;i<event[NbE].NbDelete;i++){
printf("Delete: %s\n",event[NbE].delete[i]);
}
}
bool Test=0; //Not complete
while(Test==0){
Test=1;
for(i=0;i<NbStart;i++){
for(k=0;k<NbStart;k++){
if(start[k]==finish[i]){
Z++;
if(Z==NbFinish){
Test=1;
}
}
}
}
Z=0;
}
if(Test==1){
printf("Le résultat a était trouvé en %d étapes.\n",x);
}
fclose(txtfile);
}
Input:
start:at door,on floor,has ball,hungry,chair at door,
finish:not hungry,
****
action:climb on chair,
preconds:chair at middle room,at middle room,on floor,
add:at bananas,on chair,
delete:at middle room,on floor,
****
action:push chair from door to middle room,
preconds:chair at door,at door,
add:chair at middle room,at middle room,
delete:chair at door,at door,
****
action:walk from door to middle room,
preconds:at door,on floor,
add:at middle room,
delete:at door,
****
action:grasp bananas,
preconds:at bananas,empty handed,
add:has bananas,
delete:empty handed,
****
action:drop ball,
preconds:has ball,
add:empty handed,
delete:has ball,
****
action:eat bananas,
preconds:has bananas,
add:empty handed,not hungry,
delete:has bananas,hungry,
Output:
Fichier ouvert.
Condition Départ:
- at door
- on floor
- has ball
- hungry
- chair at door
Condition Fin:
- not hungry
Precond: chair at middle room
Precond: at middle room
Precond: on floor
Add: at bananas
Add: on chair
Delete: at middle room
Delete: on floor
Precond: chair at doorle room
Precond: at doorle room
Add: chair at middle room
Add: at middle room
Delete: chair at doorm
Delete: at doorr
Precond: at doort doorle room
Precond: on floore room
Add: at middle roome room
Delete: at doort doorm
Precond: at bananasoorle room
Precond: empty handedom
Add: has bananasoome room
Delete: empty handedrm
Precond: has ballasoorle room
Add: empty handedome room
Delete: has ballndedrm
Precond: has bananasorle room
Add: empty handedome room
Add: not hungryroom
Delete: has bananasdrm
Delete: hungryrr
Le résultat a était trouvé en 0 étapes.