我提取的.txt文件出现奇怪的错误

问题描述 投票:0回答:0

我正在尝试恢复 .txt 文件文本以将其转换为记录以创建基本的“gps”。 我确实设法将文本变成了一条记录,但是,代码运行的时间越长,出现的错误就越频繁(例如它写“hungrygry”而不是“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);
}

How it ends up

txt file input exemple

如果有人能解释如何解决这个问题,我将非常感激。

我试着寻找这种类型的错误,但从未发现类似的情况,所以我真的不知道该怎么办...

c string record
© www.soinside.com 2019 - 2024. All rights reserved.