我在C中创建一个程序。我想让这个程序反转一个txt文件,avec 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;
}
有我的pile.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]
我是个乞丐......也许我做得不好。 谢谢你的回复。
约旦
答案 0 :(得分:0)
char *chaine = malloc(sizeof(*chaine));
=&gt;此处为sizeof(*chaine) == 1
,因为类型为char *
,因此指向的类型为char
和sizeof(char) == 1
。你可能想要char *chaine = (char *) malloc(strlen(elementDepile->donnee) * sizeof(char));
- Fefux