C Program that makes a copy of a file using standard I/O and system calls

C Program that makes a copy of a file using standard I/O and system calls

A simple C program that makes a copy of a file using both standard I/O and system calls. In this example, We'll use fopen, fread, fwrite, fclose for standard I/O, and open, read, write, close for system calls.

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #define BUFFER_SIZE 1024 void copyFileStandardIO(const char *source, const char *destination) { FILE *sourceFile = fopen(source, "rb"); FILE *destFile = fopen(destination, "wb"); if (sourceFile == NULL || destFile == NULL) { perror("Error opening files"); exit(EXIT_FAILURE); } char buffer[BUFFER_SIZE]; size_t bytesRead; while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) { fwrite(buffer, 1, bytesRead, destFile); } fclose(sourceFile); fclose(destFile); } void copyFileSystemCalls(const char *source, const char *destination) { int sourceFile = open(source, O_RDONLY); int destFile = open(destination, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (sourceFile == -1 || destFile == -1) { perror("Error opening files"); exit(EXIT_FAILURE); } char buffer[BUFFER_SIZE]; ssize_t bytesRead; while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0) { write(destFile, buffer, bytesRead); } close(sourceFile); close(destFile); } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source_file> <destination_file>\n", argv[0]); exit(EXIT_FAILURE); } // Using standard I/O copyFileStandardIO(argv[1], argv[2]); printf("File copied using standard I/O.\n"); // Using system calls copyFileSystemCalls(argv[1], argv[2]); printf("File copied using system calls.\n"); return 0; } 

This program takes two command-line arguments: the source file and the destination file. It then copies the contents of the source file to the destination file using both standard I/O and system calls. You can compile this program using a C compiler like gcc. For example:

gcc copyfile.c -o copyfile 

And then run it:

./copyfile source.txt destination.txt 

Examples

  1. "C program to copy a file using standard I/O"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; char ch; source = fopen("source.txt", "r"); destination = fopen("destination.txt", "w"); while ((ch = fgetc(source)) != EOF) { fputc(ch, destination); } fclose(source); fclose(destination); return 0; } 
    • Description: Uses standard I/O functions (fopen, fgetc, fputc, fclose) to copy the contents of one file to another.
  2. "C program to copy a file using read and write system calls"

    • Code:
      #include <fcntl.h> #include <unistd.h> int main() { int source, destination; char buffer[4096]; ssize_t bytesRead; source = open("source.txt", O_RDONLY); destination = open("destination.txt", O_WRONLY | O_CREAT, 0666); while ((bytesRead = read(source, buffer, sizeof(buffer))) > 0) { write(destination, buffer, bytesRead); } close(source); close(destination); return 0; } 
    • Description: Uses system calls (open, read, write, close) to copy the contents of one file to another.
  3. "C program to copy a binary file using fread and fwrite"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; size_t bytesRead; char buffer[4096]; source = fopen("source.bin", "rb"); destination = fopen("destination.bin", "wb"); while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) { fwrite(buffer, 1, bytesRead, destination); } fclose(source); fclose(destination); return 0; } 
    • Description: Uses fread and fwrite to copy the contents of a binary file using standard I/O.
  4. "C program to copy a file with error handling"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; char ch; source = fopen("source.txt", "r"); destination = fopen("destination.txt", "w"); if (!source || !destination) { perror("Error opening files"); return 1; } while ((ch = fgetc(source)) != EOF) { if (fputc(ch, destination) == EOF) { perror("Error writing to file"); return 1; } } fclose(source); fclose(destination); return 0; } 
    • Description: Adds error handling to check if file opening and writing operations are successful.
  5. "C program to copy a file with command-line arguments"

    • Code:
      #include <stdio.h> int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source_file> <destination_file>\n", argv[0]); return 1; } FILE *source, *destination; char ch; source = fopen(argv[1], "r"); destination = fopen(argv[2], "w"); // Rest of the code remains the same return 0; } 
    • Description: Modifies the program to accept source and destination file names as command-line arguments.
  6. "C program to copy a file using buffered I/O functions"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; char buffer[4096]; size_t bytesRead; source = fopen("source.txt", "rb"); destination = fopen("destination.txt", "wb"); while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) { fwrite(buffer, 1, bytesRead, destination); } fclose(source); fclose(destination); return 0; } 
    • Description: Uses buffered I/O functions (fread, fwrite) to copy the contents of one file to another.
  7. "C program to copy a file and display progress"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; char buffer[4096]; size_t bytesRead, totalBytes = 0; source = fopen("source.txt", "rb"); destination = fopen("destination.txt", "wb"); while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) { fwrite(buffer, 1, bytesRead, destination); totalBytes += bytesRead; printf("\rBytes copied: %zu", totalBytes); } fclose(source); fclose(destination); printf("\nCopy complete\n"); return 0; } 
    • Description: Enhances the program to display progress information while copying.
  8. "C program to copy a file using mmap"

    • Code:
      #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> int main() { int source, destination; struct stat statSource; char *mapSource, *mapDestination; source = open("source.txt", O_RDONLY); fstat(source, &statSource); destination = open("destination.txt", O_RDWR | O_CREAT, 0666); ftruncate(destination, statSource.st_size); mapSource = mmap(NULL, statSource.st_size, PROT_READ, MAP_PRIVATE, source, 0); mapDestination = mmap(NULL, statSource.st_size, PROT_WRITE, MAP_SHARED, destination, 0); memcpy(mapDestination, mapSource, statSource.st_size); munmap(mapSource, statSource.st_size); munmap(mapDestination, statSource.st_size); close(source); close(destination); return 0; } 
    • Description: Uses memory-mapped I/O (mmap) to copy the contents of one file to another.
  9. "C program to copy a file with overwrite confirmation"

    • Code:
      #include <stdio.h> int main() { FILE *source, *destination; char ch, response; source = fopen("source.txt", "r"); destination = fopen("destination.txt", "w"); if (!source || !destination) { perror("Error opening files"); return 1; } printf("Destination file exists. Overwrite? (y/n): "); scanf(" %c", &response); if (response != 'y' && response != 'Y') { fclose(source); fclose(destination); return 0; } while ((ch = fgetc(source)) != EOF) { if (fputc(ch, destination) == EOF) { perror("Error writing to file"); return 1; } } fclose(source); fclose(destination); return 0; } 
    • Description: Adds a confirmation prompt to ask whether to overwrite the destination file.
  10. "C program to copy a file using memory allocation"

    • Code:
      #include <stdio.h> #include <stdlib.h> int main() { FILE *source, *destination; long fileSize; char *buffer; source = fopen("source.txt", "rb"); destination = fopen("destination.txt", "wb"); fseek(source, 0, SEEK_END); fileSize = ftell(source); rewind(source); buffer = (char *)malloc(fileSize); fread(buffer, 1, fileSize, source); fwrite(buffer, 1, fileSize, destination); free(buffer); fclose(source); fclose(destination); return 0; } 
    • Description: Allocates memory to read the entire file into a buffer and then writes the buffer to the destination file.

More Tags

django-cache lemmatization opacity functional-programming loadimage xts recyclerview-layout ora-00904 ansible-facts class-extensions

More Programming Questions

More Statistics Calculators

More Mixtures and solutions Calculators

More Organic chemistry Calculators

More Housing Building Calculators