Pgm read write

  • Exemple de programme read write ecran


#include <unistd.h>
#include <stdio.h>


int main(void)
{
	int choix;
	FILE *saisie;
	FILE *sortie;
	saisie = fopen("/dev/tty","r");
	sortie = fopen("/dev/tty","w");
	do{
		fprintf(sortie,"Frappez une touche, puis validez.(touche q pour sortir)\n");
		do{
				choix=fgetc(saisie);
		} while(choix== '\n');
		fprintf(sortie,"Vous avez appuyé sur la touche %c\n",choix);
	}while(choix!='q');
	fclose(saisie);
	fclose(sortie);
}
  • Exemple de programme read fichier


***** equivalent en awk 
 awk -F: '{ print $1 }' /etc/passwd
******************************************

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

#define MAX_LINE_LEN 1024

int main () {
    char line [MAX_LINE_LEN];
    FILE *in = fopen ("/etc/passwd", "r");
    if (!in) exit (EXIT_FAILURE);
    while (fgets(line, MAX_LINE_LEN, in) != NULL) {
        char *sep = strchr(line , ':');
        if (!sep) exit (EXIT_FAILURE);
        *sep = '\0';
        printf("%s\n", line);
    }
    fclose(in);
    return EXIT_SUCCESS ;
}


Previous page: Pgm ncurses read fic
Page suivante : Pgm Tableau