-
-
Save tamago324/adb6c141e97943817154cb7b8ab0c288 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
#include <stdio.h> | |
#include <stdlib.h> | |
// 第1引数のファイルを第2引数のファイルにコピーする | |
// 第2引数のファイルが存在しない場合、作成する | |
int main(int argc, char *argv[]) { | |
FILE *wf; | |
FILE *rf; | |
int c; | |
if (argc != 3) { | |
printf("第1引数にコピー元のファイル、第2引数にコピー先のファイルを指定してください\n"); | |
exit(1); | |
} | |
rf = fopen(argv[1], "r"); | |
if (!rf) { | |
perror(argv[0]); | |
exit(1); | |
} | |
wf = fopen(argv[2], "w"); | |
while ((c = fgetc(rf)) != EOF) { | |
if (fputc(c, wf) < 0) exit(1); | |
} | |
fclose(rf); | |
fclose(wf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment