Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created January 25, 2018 16:13
Show Gist options
  • Save tamago324/adb6c141e97943817154cb7b8ab0c288 to your computer and use it in GitHub Desktop.
Save tamago324/adb6c141e97943817154cb7b8ab0c288 to your computer and use it in GitHub Desktop.
ファイルをコピーする
#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