scr
This commit is contained in:
56
SCR1.2/TP11/read_file.c
Normal file
56
SCR1.2/TP11/read_file.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define CHUNK_SIZE 256
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <source_file> <destination_file>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int source_desc = open(argv[1], O_RDONLY);
|
||||
if (source_desc < 0)
|
||||
{
|
||||
perror("Erreur lors de l'ouverture du fichier source");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int dest_desc = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (dest_desc < 0)
|
||||
{
|
||||
perror("Erreur lors de l'ouverture/la création du fichier destination");
|
||||
close(source_desc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char buffer[CHUNK_SIZE];
|
||||
ssize_t bytes_read;
|
||||
|
||||
while ((bytes_read = read(source_desc, buffer, sizeof(buffer))) > 0)
|
||||
{
|
||||
if (write(dest_desc, buffer, bytes_read) < 0)
|
||||
{
|
||||
perror("Erreur lors de l'écriture");
|
||||
close(source_desc);
|
||||
close(dest_desc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
perror("Erreur lors de la lecture du fichier source");
|
||||
close(source_desc);
|
||||
close(dest_desc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
close(source_desc);
|
||||
close(dest_desc);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user