C程序崩溃逆文件txt

标签 c

我正在用 C 创建一个程序。我希望这个程序反转 txt 文件,保存 geojson 坐标。

程序运行良好,但是当txt文件太长时,程序崩溃......! 我认为内存有问题,但我不知道如何解决它。

提前致谢!

这是我的 main.c :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pile.h"
#include "pile_function.h"

#define TAILLE_MAX 5000 // Tableau de taille 1000




int main(int argc, char *argv[]) {

FILE* fichier = NULL;
FILE* fichier_creer = NULL;
char chaine[TAILLE_MAX] = ""; // Chaîne vide de taille TAILLE_MAX
char concat[TAILLE_MAX] = "";
char final[TAILLE_MAX] = "";
const char s[2] = "]";

 char *token = malloc(sizeof(*token));
 Pile *tas;

 if ((tas = (Pile *) malloc (sizeof (Pile))) == NULL)
     return -1;
 initialisation (tas);


    fichier = fopen(argv[1], "r+");
    if (fichier != NULL)

       {

           // On peut lire et écrire dans le fichier
        printf("Ouverture du fichier\n");

         fgets(chaine, TAILLE_MAX, fichier);

         fichier_creer = fopen("Fichier_Inverser.txt", "a+");
         if (fichier != NULL)
             {
             printf("Ouverture du fichier_creer\n");



               /* get the first token */
               token = strtok(chaine, s);

               /* walk through other tokens */
               while( token != NULL )
               {

                  strcpy(concat, token);
                  strcat(concat, s);


                  empiler(tas, concat);
                  token = strtok(NULL, s);
                  memset (concat, 0, sizeof (concat));
               }


                  while( tas->taille != 0 )
                  {

                      strcpy (final, depiler(tas));
                      fputs(final, fichier_creer);
                  }

              printf("\nFichier mise a jour\n");
             fclose(fichier_creer);
             printf("\nFermeture du fichier du fichier_creer\n");
             }





         else

           {
             printf("Impossible d'ouvrir le fichier_creer");
           }

         fclose(fichier);
         printf("Fermeture du fichier du fichier\n");

       }

       else

       {
           printf("Impossible d'ouvrir le fichier");

       }
return EXIT_SUCCESS;
}

这是我的一堆.h:

typedef struct ElementListe{
char *donnee;
struct ElementListe *suivant;
 } Element;

typedef struct ListeRepere{
Element *debut;
int taille;
} Pile;


/* initialisation */
void initialisation (Pile *tas);

/* EMPILER*/
int empiler (Pile *tas, char *donnee);

/* DEPILER*/
char *depiler (Pile *tas);

/* Affichage de élément en haut de la pile (LastInFirstOut) */
#define pile_donnee(tas)  tas->debut->donnee

/* Affiche la pile */
void affiche (Pile *tas);

这是我的pile_function.h:

void initialisation (Pile * tas){
tas->debut = NULL;
tas->taille = 0;
}


int empiler (Pile * tas, char *donnee){
Element *nouveau_element;
 if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL)
   return -1;
 if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL)
   return -1;
 strcpy (nouveau_element->donnee, donnee);
 nouveau_element->suivant = tas->debut;
 tas->debut = nouveau_element;
 tas->taille++;
}

char *depiler(Pile *pile)
{
 if (pile == NULL)
  {
    exit(EXIT_FAILURE);
  }
char *chaine =  malloc(sizeof(*chaine));
Element *elementDepile = pile->debut;

if (pile != NULL && pile->debut != NULL)
 {
    strcpy (chaine, elementDepile->donnee);
    pile->debut = elementDepile->suivant;
    free(elementDepile);
 }
pile->taille--;

return chaine;
}


void affiche (Pile * tas){
 Element *courant;
 int i;
 courant = tas->debut;

 for(i=0;i<tas->taille;++i){
   printf("%s\n", courant->donnee);
   courant = courant->suivant;
 }
}

例如有一个坐标列表,它不起作用:

[2.2528324,49.0413749],[2.2530099,49.0409694],[2.2529714,49.0409477],[2.2529531,49.040845],[2.2528231,49.040697],[2.2525572,49.0405152],[2.2521051,49.0402405],[2.2518017,49.0400133],[2.2515308,49.0397237],[2.2514333,49.0395455],[2.2514103,49.0394521],[2.2514134,49.0393256],[2.25172,49.0383752],[2.2517745,49.0380228],[2.2518929,49.0377766],[2.2520333,49.0375694],[2.2522566,49.0373093],[2.2523922,49.0372076],[2.2525084,49.036936],[2.2528797,49.0363597],[2.2529077,49.0362346],[2.2528555,49.0359416],[2.2527984,49.0358494],[2.2527631,49.0358471],[2.2494004,49.0368099],[2.2445056,49.0382113],[2.2438535,49.0351289],[2.2434025,49.0334159],[2.2433668,49.0333207],[2.2424539,49.0292753],[2.242455,49.0290301],[2.2425994,49.0285152],[2.2425996,49.0284322],[2.241938,49.0267597],[2.241008,49.0254301],[2.2405995,49.0251103],[2.2405338,49.0250148],[2.2404128,49.0247205],[2.2403438,49.0244781],[2.2403436,49.0243775],[2.239998,49.0235028],[2.2399302,49.0233991],[2.2398091,49.0233274],[2.2397032,49.0232808],[2.2395594,49.0232176],[2.2394263,49.0231172],[2.2393327,49.0230396],[2.2392098,49.0229535],[2.2387176,49.0225323],[2.238216,49.0221354],[2.237813,49.0218217],[2.2375089,49.0214585],[2.2373633,49.0215158],[2.2371741,49.0213435],[2.2364466,49.0204618],[2.2363631,49.0202973],[2.2359734,49.0197239],[2.2358766,49.0195764],[2.23573,49.0192646],[2.2356873,49.0192694],[2.235498,49.0189371],[2.2354933,49.0189123],[2.2352065,49.0184121],[2.23519,49.0184137],[2.2350145,49.0184304],[2.2348705,49.0184441],[2.2342795,49.0177464],[2.2340851,49.017802],[2.2338829,49.0175392],[2.2338473,49.017546],[2.2331775,49.0168764],[2.2327003,49.0163514],[2.2326684,49.0163499],[2.231627,49.0154023],[2.2312705,49.0150763],[2.2311292,49.0149744],[2.2302659,49.0144945],[2.2301856,49.0144539]

但是使用相同的列表但很短,它可以工作:

[2.2528324,49.0413749],[2.2530099,49.0409694],[2.2529714,49.0409477],[2.2529531,49.040845],[2.2528231,49.040697],[2.2525572,49.0405152],[2.2521051,49.0402405],[2.2518017,49.0400133],[2.2515308,49.0397237],[2.2514333,49.0395455],[2.2514103,49.0394521],[2.2514134,49.0393256],[2.25172,49.0383752],[2.2517745,49.0380228],[2.2518929,49.0377766],[2.2520333,49.0375694],[2.2522566,49.0373093],[2.2523922,49.0372076],[2.2525084,49.036936],[2.2528797,49.0363597]

我是一个初学者..所以也许我做得不好。 谢谢您的回复。

约旦

最佳答案

char *chaine = malloc(sizeof(*chaine)); => 这里 sizeof(*chaine) == 1 因为类型是 char * 所以指向的类型是 charsizeof(char) == 1。您可能想要 char *chaine = (char *) malloc(strlen(elementDepile->donnee) * sizeof(char));
– 菲福克斯

关于C程序崩溃逆文件txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972220/

相关文章:

c - 如何将数组包含到 C 函数定义中

CURAND 和内核,在哪里生成?

编译单独的内核模块 (Debian/Ubuntu)

C:处理 HTTP 请求和响应

c - 为什么 `volatile` 没有区别?

c - 单链表简单数据库

c - 枚举值的字符串表示

c - C中这个表达式的返回类型是什么?

转换指针不会产生左值。为什么?

python - 如何为 NI VeriStand 共享库传递带有 ctypes 的数组