0

Disclaimer: some words in my code are in French.

When I compile my code there is no bug, but when I run it the only thing that appears is the sentence "Contenu du tableau avant le tri". I think the file I need to read (notes.txt) isn't being read. Anyone has any idea what the problem is? Here's my code:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX_PERS 200



typedef struct
{
    int numero;
    float note;
    char nom, prenom;
}   donnees;

donnees tab[MAX_PERS];
int nbPers=0;



void lire(donnees tab[], int *n)
{
    FILE *aLire = fopen("notes.txt", "r");

    int nb=0;

    while(!feof(aLire))
    {
        fscanf(aLire, "%d%s%s%f\n", &tab[nb].numero, &tab[nb].nom, &tab[nb].prenom, &tab[nb].note);
        nb++;  
    }   
    fclose(aLire);
    *n = nb;   
}



void afficher(donnees tab[], int nb, char *quand)
{
    int i;

    printf("Contenu du tableau %s le tri\n\n", quand);

    for(i=0; i<nb; i++)
        printf( "%4d %15s %15s %2.2f\n", tab[i].numero, tab[i].nom, tab[i].prenom, tab[i].note);
}



void echanger (donnees *P1, donnees *P2)
{ 
    donnees tempo;

    tempo = *P1 ;
    *P1 = *P2;
    *P2 = tempo;
}



void  partitionner ( donnees tab[], int debut, int fin, int *P )
{ 
    int G = debut , D = fin  ;

    float Val_Pivot = tab[debut].note;

    while ( G <= D  &&  tab[G].note <= Val_Pivot) G++;

    while ( tab[D].note > Val_Pivot) D--;

        if ( G < D ) echanger(&tab[G], &tab[D]);

    while ( G <= D ) ;

        *P = D ;

        echanger (&tab[debut], &tab[D]);
}



void quickSort ( donnees tab[], int gauche, int droite )
{ 
    int indPivot ;

    if (droite > gauche)
        {
            partitionner ( tab, gauche, droite, &indPivot);
            quickSort ( tab, gauche, indPivot - 1 );
            quickSort ( tab, indPivot + 1, droite);
        }
}




int main()
{
    donnees tab[MAX_PERS];

    lire(tab, &nbPers);

    afficher(tab, nbPers, "avant");



    return 0;
}
  • 3
    Please provide an example of the file contents. For starters, check the return values of `fopen` and `fscanf`. Also, when a program doesn't work as expected what you should do is to debug it by running in a debugger or adding debug print statements. Have you done any of that and if so what did you find out about where things start going wrong? – kaylum Dec 11 '19 at 04:21
  • 1
    If you are not sure if the file is actually being opened, try `if (!aLire) { /* error */ }` after `fopen` to check if `aLire` points to an address instead of NULL. – Henry Fung Dec 11 '19 at 04:22
  • You could check for value of nb at the end of function lire() , a simple printf to check if that's working as expected. Then check it again inside afficher(). Bon courage. – BobRun Dec 11 '19 at 04:25
  • Welcome to Stack Overflow! [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Dec 11 '19 at 04:52
  • 1
    In your `partitionner` function, it seems unlikely that `while ( G <= D ) ;` is correct - this is an infinite loop (or a no-op). Does the `;` at the end belong. – Steve Friedl Dec 11 '19 at 04:54

1 Answers1

0

Verify fopen return result, as it returns NULL in case opening fail for some reason.

don't rely on !feof for detecting the end of the file, as your file can end partially, use fscanf return value instead (number of entry parsed/read )

then in your structure your are using char (one character) for name, where you should either have char[some length] or char*

int numero;
float  note;
char[MAX_LENGTH] nom;
char[MAX_LENGTH] prenom;
// assuming that field are separated by space
while(fscanf(
"%d %"MAX_LENGTH"s %"MAX_LENGTH"s %f",
&numero,
nom,
prenom,
&note) == 4) {
   // copy data in new row in array
}
dvhh
  • 4,724
  • 27
  • 33
  • 1
    Thank you so much! I changed `char nom` and `char prenom` to `char nom[MAX_LENGTH]` and `char prenom[MAX_LENGTH]` and now it finally works. Had been stuck for two days, thank you! –  Dec 11 '19 at 06:12