/*
 * Cours Langage C - AIR1
 * C. Recanati 27/08/2019 12:43
 * TP4 exo1
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define MAX_SIZE_LINE 240

int main() {	
	char nom_fichier[40];
	char ligne[MAX_SIZE_LINE];

 	char *s;	
 	FILE *fichier;

	printf("Entrez un nom de fichier texte: ");
	scanf("%s", nom_fichier);
 	fichier = fopen(nom_fichier, "r");
 	if (fichier == NULL) {
 		printf("impossible d'ouvrir %s ! erreur %d: %s\n", 
 					nom_fichier, errno, strerror(errno));
 		return errno;
 	}
	s = (char *) malloc(sizeof(char) * MAX_SIZE_LINE);
	ligne[MAXSIZE-1]='\0';
	while ((s = fgets(ligne, MAX_SIZE_LINE, fichier)) != 0) {
		// comme le return de fin de ligne ne nous interesse pas  
		// on peut le remplacer par le caractere nul de fin de chaine
		ligne[strlen(ligne)-1] = '\0';
		printf("ligne lue: %s longueur: %lu\n", ligne, strlen(ligne));
	}
	if (fclose(fichier) == EOF)
		printf("erreur de fermeture du fichier %s\n", nom_fichier);
	return EXIT_SUCCESS;
}
