Skip to content

dinwisp/File_IO

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Title: C File Copy Function (fcopy)

Description:

This C code implements a function named fcopy that efficiently copies the contents of one file to another. It handles potential errors during file opening and closing, providing informative return codes to indicate success or specific issues encountered.

Key Features:

Cross-Platform Compatibility: Leverages standard C library functions (fopen_s, getc, putc, fclose) for portability across various operating systems. Error Handling: Checks return values of fopen_s for both source and destination files. Returns specific error codes (1, -1) to signal source/destination file opening failures. Verifies fclose return values for both files. Returns additional error codes (2, -2) to indicate issues during closing. Efficient Character-by-Character Copying: Employs a while loop to extract characters from the source file using getc and write them to the destination file using putc. Usage:

C {

  #include "file.h" // Assuming "file.h" contains necessary header declarations

  int main() {

      const char* sourceFile = "source.txt";
      const char* destinationFile = "copy.txt";

      int copyStatus = fcopy(sourceFile, destinationFile);

      if (copyStatus == 0) {
          printf("File copied successfully!\n");
      } else {
          switch (copyStatus) {
              case 1:
                  printf("Error: Could not open source file.\n");
                  break;
              case -1:
                  printf("Error: Could not open destination file.\n");
                  break;
              case 2:
                  printf("Error: Could not close destination file.\n");
                  break;
              case -2:
                  printf("Error: Could not close source file.\n");
                  break;
              default:
                  printf("Unknown error occurred during file copy.\n");
          }
        }
  
      return 0;
      }

}

Considerations:

Ensure the file.h header file is included if it contains necessary declarations for fopen_s, getc, putc, and fclose. Modify sourceFile and destinationFile variables in the example main function to reflect the actual file paths you want to use.

About

includes code to do I/O file operations in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages