Files
UTEC-Lic_ME_2024_2025/UTC502/24-10-17/TD.c
T
Guillaume-Sanchez ff4bb12d22 initial commit
2026-05-26 13:56:03 +02:00

49 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/****************************************************************/
/* Exemple de manipulation dun fichier: */
/* création, positionnement, fermeture */
/****************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct student
{
char nom[10];
int note;
} eleve;
int main()
{
int fd, i, ret;
eleve un_eleve;
fd = open("./eleves.txt", O_CREAT | O_TRUNC | O_RDWR , S_IRUSR | S_IRGRP);
if (fd == -1)
perror("prob open");
i = 0;
while (i < 4)
{
printf("Donnez le nom de l’élève \n");
scanf("%s", un_eleve.nom);
printf("Donnez la note de l’élève \n");
scanf("%d", &un_eleve.note);
write(fd, &un_eleve, sizeof(eleve));
i = i + 1;
}
ret = lseek(fd, SEEK_SET, 0);
if (ret == -1)
perror("prob lseek");
printf("la nouvelle position est %d\n", ret);
i = 0;
while (i < 4)
{
read(fd, &un_eleve, sizeof(eleve));
printf("le nom et la note de l’élève sont %s, %d\n",
un_eleve.nom,
un_eleve.note);
i = i + 1;
}
close(fd);
return 0;
}