Trier une structure avec tri bulle en c
acrony
-
crazycode Messages postés 43 Statut Membre -
crazycode Messages postés 43 Statut Membre -
Bonjour,
je voudrais lire un fichier texte, a partir d'un programme en C, récupérer des données en utilisant une structure, trier la structure avec la méthode tri bulle et enregistrer le tableau trié dans un nouveau fichier texte que j'aurais créé et l'afficher a l'exécution du programme. Voici mon code, pleins d'erreur puisqu'à la compilation le compilateur m'envoie une erreur, soyez indulgents s'il y a de grosses incongruités. je compte sur vous...
#include <stdio.h>
#include <stdlib.h>
//#define MAX 1000 // max size 1000
struct typeliste
{
int num;
char name[10];
int month;
int day;
};
int main()
{
FILE *p_fi;//this file will receive my structure after the sort
char name_fi;// the file name
int k;
void tri_tableau (int *tab[], int *taille)
{
int i,j;
int temp;
/* bubble sort of the array */
for (i=0; i<taille-1; i++)
for (j=0; j<taille-1; j++)
if (tab[j]>tab[j+1]) /* changing of values*/
{ temp=tab[j];
tab[j]=tab[j+1];
tab[j+1]=temp;
}
}
FILE* fil = NULL;
// Var
int ix;
int TAB_LONG=10;
struct typeliste liste[10];
fil = fopen("birth.txt", "r");
// Saisie
for(ix=0; ix<TAB_LONG; ix++)
{
if (fil != NULL)
{
fscanf(fil, "%d %s %d %d", &liste[ix].num, liste[ix].name, &liste[ix].day,&liste[ix].month);
}tri_tableau (liste[ix].num, TAB_LONG);
p_fi = fopen(name_fi, "w");
if (p_fi == NULL)
{
printf("Error \n");
exit(-1); // leave if error
}
for (k = 0; k<TAB_LONG; k++)
{
fprintf(p_fi, "%s\n", liste[ix]);
}
fclose(p_fi);
/* reading of the file */
p_fi = fopen(name_fi,"r"); /* read */
if (p_fi== NULL)
{
printf("\opening error \n");
exit(-2); // leave if error
}
while (!feof(p_fi))
{
fscanf(p_fi, "%s\n", name_fi);
printf(p_fi, "%s\n", name_fi);
}
fclose(p_fi);
}
}
fclose(fil);
je voudrais lire un fichier texte, a partir d'un programme en C, récupérer des données en utilisant une structure, trier la structure avec la méthode tri bulle et enregistrer le tableau trié dans un nouveau fichier texte que j'aurais créé et l'afficher a l'exécution du programme. Voici mon code, pleins d'erreur puisqu'à la compilation le compilateur m'envoie une erreur, soyez indulgents s'il y a de grosses incongruités. je compte sur vous...
#include <stdio.h>
#include <stdlib.h>
//#define MAX 1000 // max size 1000
struct typeliste
{
int num;
char name[10];
int month;
int day;
};
int main()
{
FILE *p_fi;//this file will receive my structure after the sort
char name_fi;// the file name
int k;
void tri_tableau (int *tab[], int *taille)
{
int i,j;
int temp;
/* bubble sort of the array */
for (i=0; i<taille-1; i++)
for (j=0; j<taille-1; j++)
if (tab[j]>tab[j+1]) /* changing of values*/
{ temp=tab[j];
tab[j]=tab[j+1];
tab[j+1]=temp;
}
}
FILE* fil = NULL;
// Var
int ix;
int TAB_LONG=10;
struct typeliste liste[10];
fil = fopen("birth.txt", "r");
// Saisie
for(ix=0; ix<TAB_LONG; ix++)
{
if (fil != NULL)
{
fscanf(fil, "%d %s %d %d", &liste[ix].num, liste[ix].name, &liste[ix].day,&liste[ix].month);
}tri_tableau (liste[ix].num, TAB_LONG);
p_fi = fopen(name_fi, "w");
if (p_fi == NULL)
{
printf("Error \n");
exit(-1); // leave if error
}
for (k = 0; k<TAB_LONG; k++)
{
fprintf(p_fi, "%s\n", liste[ix]);
}
fclose(p_fi);
/* reading of the file */
p_fi = fopen(name_fi,"r"); /* read */
if (p_fi== NULL)
{
printf("\opening error \n");
exit(-2); // leave if error
}
while (!feof(p_fi))
{
fscanf(p_fi, "%s\n", name_fi);
printf(p_fi, "%s\n", name_fi);
}
fclose(p_fi);
}
}
fclose(fil);
A voir également:
- Trier une structure avec tri bulle en c
- Comment trier par ordre alphabétique sur excel - Guide
- Logiciel tri photo - Guide
- Logiciel calcul structure bois gratuit - Télécharger - Architecture & Déco
- Logiciel pour trier les photos - Forum Canon
- Le fichier contient une liste de prénoms. triez ce tableau par ordre alphabétique des prénoms - Forum LibreOffice / OpenOffice