Q:

Read and write from file in C langage

Please, some one can explain for me why this program does not work ?
I'm trying to read and write from the file by using r+. The file test.txt exists and the writing is executed correctly. However, the reading does not work.
int main() {

    FILE* fichier = NULL;
    int age, TAILLE_MAX=10;
    char chaine[TAILLE_MAX];

    fichier = fopen("test.txt", "r+");
    if (fichier != NULL)
    {

        printf("give your age ? ");
        scanf("%d", &age);
        fprintf(fichier, "Hello you have %d year old", age);

        while (fgets(chaine, TAILLE_MAX, fichier) != NULL)
        {
            printf("%s", chaine); //does not print
        }fclose(fichier);
    }

    return 0;
}

By does not work I mean it does not display any thing ! Even if the file contains some the sentence you have ... year old. There is no Error. Just the program does not print the content of the file

A:

you are writing and reading to file at the same time,
which is not good practice,
but the reason your code does not work is because of buffering.  The fprintf(fichier, "Hello you have %d year old", age); is likely not happening until your fclose(fichier) statement is happening.
I added the two statements to your code, see below.
Also once you do your fprintf your file pointer fichier is not at the end of the file which is the wrong place for the next thing you try to do which is read the age number you just wrote, so you have to move the file pointer fichier back somehow - I just used rewind which will work if test.txt was a newly created file.  Otherwise you will need some method of moving the file pointer fichier backwards just enough to read what you just wrote.
int main() {

FILE* fichier = NULL;
int age, TAILLE_MAX=10;
char chaine[TAILLE_MAX];

fichier = fopen("test.txt", "r+");
if (fichier != NULL)
{

    printf("give your age ? ");
    scanf("%d", &age);
    fprintf(fichier, "Hello you have %d year old", age);

    fflush( fichier );  /* force  write to FILE */
    rewind( fichier );  /* rewind FILE pointer to beginning */

    while (fgets(chaine, TAILLE_MAX, fichier) != NULL)
    {
        printf("%s", chaine); //does not print
    }
}
fclose(fichier);
return 0;
}

in your original code, the statement
while (fgets(chaine, TAILLE_MAX, fichier) != NULL)

fails to read anything and returns NULL, so the printf("%s", chaine); will not happen.  This is happening because of output buffering and the fprintf() statement not happening when you think it should.
This output buffering is normal and if you want a printf to happen at that exact moment then you need to use fflush()
read here to learn more: Why does printf not flush after the call unless a newline is in the format string?

