solution_10_1_file_copy.c
C Programming Language/solutions/advanced/week10/solution_10_1_file_copy.c
/**
* Solution 10.1: File Copying Utility
* Week 10 - File Input/Output
*
* Description: File copying utility
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}
FILE *source = fopen(argv[1], "rb");
if (source == NULL) {
printf("Error: Cannot open source file '%s'\n", argv[1]);
return 1;
}
FILE *destination = fopen(argv[2], "wb");
if (destination == NULL) {
printf("Error: Cannot create destination file '%s'\n", argv[2]);
fclose(source);
return 1;
}
int ch;
long bytescopied = 0;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
bytescopied++;
}
fclose(source);
fclose(destination);
printf("File copied successfully!\n");
printf("Bytes copied: %ld\n", bytescopied);
return 0;
}
Artículos relacionados
bitwise_operations_advanced.c
bitwise_operations_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/bitwise_operations_advanced.c).
Leer artículo →huffman_coding_complete.c
huffman_coding_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/huffman_coding_complete.c).
Leer artículo →recursion_advanced.c
recursion_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/recursion_advanced.c).
Leer artículo →sorting_algorithms_complete.c
sorting_algorithms_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/sorting_algorithms_complete.c).
Leer artículo →binary_trees_complete.c
binary_trees_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/binary_trees_complete.c).
Leer artículo →linked_lists_complete.c
linked_lists_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/linked_lists_complete.c).
Leer artículo →