TP-systeme-exploitation-cen.../TP3/verif_fichiers/etu/scruter.c
2023-06-20 20:57:09 +02:00

56 lines
1.1 KiB
C

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
int main() {
extern int errno;
char file[] = "temp";
// opening file
int desc_f = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (desc_f < 0) {
fprintf(stderr, "ERROR: opening %s : %s\n", file, strerror(errno));
exit(errno);
}
// writing to file
int ret_write = 0;
for (int i = 1; i <= 30; i++) {
ret_write = write(desc_f, &i, sizeof(int));
if (ret_write < 0) {
fprintf(stderr, "ERROR: opening %s : %s\n", file, strerror(errno));
exit(errno);
}
printf("%02d ", i); // debug
fflush(stdout); // debug
sleep(1);
if (i % 10 == 0) {
lseek(desc_f, 0, SEEK_SET);
printf("\r");
}
}
printf("\n");
// closing files
int ret_close = 0;
ret_close = close(desc_f);
if (ret_close < 0) {
fprintf(stderr, "ERROR: closing %s : %s\n", file, strerror(errno));
exit(errno);
}
return EXIT_SUCCESS;
}