Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************
- * Class: MAGSHIMIM C2 *
- * Name: Itay *
- **********************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NUM_OF_ARGS 4
- #define MODE_PARAM (*(argv + 1))
- #define SRC_PARAM (*(argv + 2))
- #define DST_PARAM (*(argv + 3))
- #define MODE_TXT 1
- #define MODE_BIN 2
- #define FALSE 0
- #define TRUE 1
- int fileExists(char* fileName)
- {
- FILE* pf = 0;
- if (pf = fopen(fileName, "r"))
- {
- fclose(pf);
- return TRUE;
- }
- return FALSE;
- }
- void checkInput(int argc, char** argv)
- {
- if (argc != NUM_OF_ARGS) // if there are not enough args
- {
- printf("Bad arguments!\n");
- printf("Press Enter to continue...\n");
- getchar();
- exit(0);
- }
- if (strcmp(MODE_PARAM, "textCopy") && strcmp(MODE_PARAM, "binaryCopy")) // if the first param is not a valid action
- {
- printf("Bad arguments!\n");
- printf("Press Enter to continue...\n");
- getchar();
- exit(0);
- }
- if (!fileExists(SRC_PARAM))
- {
- printf("Source file does not exist!\n");
- printf("Press Enter to continue...\n");
- getchar();
- exit(0);
- }
- }
- void initializeFiles(int argc, char** argv, FILE** in, FILE** out, int mode)
- {
- int choice = 0;
- if (!fileExists(SRC_PARAM)) // if the file does not exist, tell the user and exit
- {
- printf("%s file does not exist.\n", SRC_PARAM);
- getchar();
- printf("Press Enter to continue...\n");
- getchar();
- exit(0);
- }
- *in = fopen(SRC_PARAM, mode == MODE_TXT ? "r" : "rb");
- if (fileExists(DST_PARAM)) // if the file exist, ask the user what to do
- {
- printf("Do you want to overwrite? 0 (no) / 1 (yes)\n");
- scanf("%d", &choice);
- getchar();
- if (choice == FALSE)
- {
- fclose(*in);
- printf("Press Enter to continue...\n");
- getchar();
- exit(0);
- }
- }
- *out = fopen(DST_PARAM, mode == MODE_TXT ? "w" : "wb"); // if user want to overwrite open a new file
- }
- void copyTxt(FILE* src, FILE* dst)
- {
- char ch = 0;
- while ((ch = fgetc(src)) != EOF)
- {
- fputc(ch, dst);
- }
- }
- void copyBin(FILE* src, FILE* dst)
- {
- char byte[1] = { 0 };
- while (!feof(src))
- {
- fread(byte, sizeof(char), 1, src);
- fwrite(byte, sizeof(char), 1, dst);
- }
- }
- int main(int argc, char** argv)
- {
- FILE* fsrc = 0;
- FILE* fdst = 0;
- checkInput(argc, argv); // exits the program if input is not valid
- initializeFiles(argc, argv, &fsrc, &fdst, !strcmp(MODE_PARAM, "textCopy") ? MODE_TXT : MODE_BIN);
- !strcmp(MODE_PARAM, "textCopy") ? copyTxt(fsrc, fdst) : copyBin(fsrc, fdst); // call copyTxt() or copyBin() based on action
- fclose(fsrc);
- fclose(fdst);
- printf("Press Enter to continue...\n");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement