TP-systeme-exploitation-cen.../TP3/scruter.c

50 lines
943 B
C
Raw Normal View History

2023-06-20 18:57:09 +00:00
#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, 0640);
if (desc_f < 0) {
perror("ERROR opening f_write.txt");
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) {
perror("ERROR writing to f_write.txt");
exit(errno);
}
sleep(1);
if (i % 10 == 0) {
lseek(desc_f, 0, SEEK_SET);
}
}
// closing files
int ret_close = 0;
ret_close = close(desc_f);
if (ret_close < 0) {
perror("ERROR closing f_read.txt");
exit(errno);
}
return EXIT_SUCCESS;
}