Last active
December 26, 2015 06:49
-
-
Save chihuahua/7110015 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Makes a copy of a file. | |
| * The destination file name must not already exist. | |
| * Usage: ./executable source destination | |
| * @author Chi Zeng ([email protected]) and section-mates | |
| * Oct. 22, 2013 | |
| */ | |
| #include <stdio.h> | |
| int main(int argc, char* argv[]) | |
| { | |
| if (argc != 3) | |
| { | |
| // check for correct usage. | |
| printf("Usage: ./executable source destination"); | |
| return 1; | |
| } | |
| FILE* sourceFile = fopen(argv[1], "r"); | |
| if (!sourceFile) | |
| { | |
| printf("File %s does not exist.", argv[1]); | |
| return 1; | |
| } | |
| FILE* checkForDest = fopen(argv[2], "r"); | |
| if (checkForDest) { | |
| // if this file exists, yell at the user since we're overriding it. | |
| printf("Hey, %s exists!", argv[2]); | |
| return 1; | |
| } | |
| // close the read-only file we just made for checking. | |
| fclose(checkForDest); | |
| FILE* destFile = fopen(argv[2], "w"); | |
| for (char c = fgetc(sourceFile); c != EOF; c = fgetc(sourceFile)) | |
| { | |
| // write one char to the file at a time. | |
| fputc(c, destFile); | |
| } | |
| // close our files. | |
| fclose(sourceFile); | |
| fclose(destFile); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment