44 lines
1.0 KiB
C
44 lines
1.0 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#define CHUNK_SIZE 256
|
||
|
|
|
||
|
|
int main(int argc, char *argv[])
|
||
|
|
{
|
||
|
|
if (argc != 2) {
|
||
|
|
fprintf(stderr, "Usage: %s <file_name>\n", argv[0]);
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
int file_desc = open(argv[1], O_RDONLY);
|
||
|
|
if (file_desc < 0)
|
||
|
|
{
|
||
|
|
perror("Erreur lors de l'ouverture du fichier");
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
char buffer[CHUNK_SIZE];
|
||
|
|
ssize_t bytes_read;
|
||
|
|
|
||
|
|
while ((bytes_read = read(file_desc, buffer, sizeof(buffer))) > 0)
|
||
|
|
{
|
||
|
|
if (write(STDOUT_FILENO, buffer, bytes_read) < 0)
|
||
|
|
{
|
||
|
|
perror("Erreur lors de l'écriture");
|
||
|
|
close(file_desc);
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (bytes_read < 0)
|
||
|
|
{
|
||
|
|
perror("Erreur lors de la lecture du fichier");
|
||
|
|
close(file_desc);
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
close(file_desc);
|
||
|
|
return EXIT_SUCCESS;
|
||
|
|
}
|