Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #define BUFFERSIZE 512
- /*
- * Program Arguments:
- * program.exe [textCopy|binaryCopy] file.src file.dst
- */
- void copy(char* method, char* src, char* dst);
- int copyTextFile(char* src, char* dst);
- int copyBinaryFile(char* src, char* dst);
- int fileExists(char* filename);
- int main(int argc, char** argv)
- {
- if (argc == 4) // check for 4 args
- {
- /*
- * argv[0] = programName
- * argv[1] = textCopy/binaryCopy/..
- * argv[2] = file.src
- * argv[3] = file.dst
- */
- if (fileExists(argv[2])) // Check if file.src exists
- {
- if (fileExists(argv[3])) // Chec if file.dst already exists (overwrite request)
- {
- int overwrite = 0;
- printf("Do you want to overwrite? 0 (no) / 1 (yes)\n");
- scanf("%d", &overwrite);
- getchar();
- if (overwrite)
- {
- copy(argv[1], argv[2], argv[3]);
- }
- }
- else
- {
- copy(argv[1], argv[2], argv[3]);
- }
- }
- else
- {
- printf("%s file does not exist\n", argv[2]);
- }
- }
- else
- {
- printf("Invalid execution\n"
- "Usage: copyFile (textCopy|binaryCopy) <sourceFilename_filename> <destFilenameination_filename>\n");
- }
- }
- /// <summary>
- /// Checks if the given file already exists
- /// </summary>
- /// <param name="filename">File to check if he exists</param>
- /// <returns>1 - If exists, 0 - If not exists</returns>
- int fileExists(char* filename)
- {
- FILE* file = NULL;
- if (file = fopen(filename, "r"))
- {
- fclose(file);
- return 1;
- }
- return 0;
- }
- /// <summary>
- /// Copys SRC file to DST file
- /// </summary>
- /// <param name="src">Source file</param>
- /// <param name="dst">Destination</param>
- /// <returns>1 - if success, 0 - if something went wrong..</returns>
- int copyTextFile(char* src, char* dst)
- {
- FILE* srcFile = NULL;
- FILE* dstFile = NULL;
- char ch = ' ';
- srcFile = fopen(src, "r");
- if (srcFile == NULL)
- {
- printf("[ERROR] Can't open %s file.", src);
- return 0;
- }
- dstFile = fopen(dst, "w");
- if (dstFile == NULL)
- {
- fclose(srcFile);
- printf("[ERROR] Can't open %s file", dst);
- return 0;
- }
- while ((ch = fgetc(srcFile)) != EOF)
- {
- fputc(ch, dstFile);
- }
- fclose(srcFile);
- fclose(dstFile);
- return 1;
- }
- /// <summary>
- /// Copys SRC file to DST file
- /// </summary>
- /// <param name="src">Source file</param>
- /// <param name="dst">D</param>
- /// <returns></returns>
- int copyBinaryFile(char* src, char* dst)
- {
- char buffer[BUFFERSIZE] = { 0 };
- FILE* srcFile = NULL;
- FILE* dstFile = NULL;
- size_t bytes = 0;
- srcFile = fopen(src, "rb");
- if (srcFile == NULL)
- {
- printf("[ERROR] Can't open %s file.", src);
- return 0;
- }
- dstFile = fopen(dst, "wb");
- if (dstFile == NULL)
- {
- fclose(srcFile);
- printf("[ERROR] Can't open %s file", dst);
- return 0;
- }
- while ((bytes = fread(buffer, 1, BUFFERSIZE, srcFile)) != 0) {
- if (fwrite(buffer, 1, bytes, dstFile) != bytes) {
- return 0;
- }
- }
- fclose(srcFile);
- fclose(dstFile);
- return 1;
- }
- /// <summary>
- /// Choosing the method to copy the given files
- /// </summary>
- /// <param name="method">Binary Copy / Text Copy</param>
- /// <param name="src">Source file</param>
- /// <param name="dst">Destination File</param>
- void copy(char* method, char* src, char* dst)
- {
- if (!strcmp(method, "textCopy"))
- {
- if (copyTextFile(src, dst))
- {
- printf("Copy completed");
- }
- }
- else if (!strcmp(method, "binaryCopy"))
- {
- if (copyBinaryFile(src, dst))
- {
- printf("Copy completed");
- }
- }
- else
- {
- printf("Invalid execution\n"
- "Usage: copyFile (textCopy|binaryCopy) <sourceFilename_filename> <destFilenameination_filename>\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement