開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio 2017
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成 - 1つのことだけをうまくやる)、自分で考えてみよう(p. 139)を取り組んでみる。
自分で考えてみよう(p. 139)
コード
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[80];
FILE *in = fopen("spooky.csv", "r");
FILE *file1 = fopen("ufos.csv", "w");
FILE *file2 = fopen("disappearances.csv", "w");
FILE *file3 = fopen("others.csv", "w");
while (fscanf(in, "%79[^\n]\n", line) == 1) {
if (strstr(line, "UFO"))
fprintf(file1, "%s\n", line);
else if (strstr(line, "Disappearance"))
fprintf(file2, "%s\n", line);
else
fprintf(file3, "%s\n", line);
}
fclose(in);
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
入出力結果(コマンドプロンプト)
C:>categorize.exe C:>type spooky.csv 30.685163,-68.137207,Type=Yeti 28.304380,-74.575195,Type=UFO 29.132971,-71.136475,Type=Ship 28.343065,-62.753906,Type=Elvis 27.868217,-68.005371,Type=Goatsucker 30.496017,-73.333740,Type=Disappearance 26.224447,-71.477051,Type=UFO 29.401320,-66.027832,Type=Ship 37.879536,-69.477539,Type=Elvis 22.705256,-68.192139,Type=Elvis 27.166695,-87.484131,Type=Elvis C:>type ufos.csv 28.304380,-74.575195,Type=UFO 26.224447,-71.477051,Type=UFO C:>type disappearances.csv 30.496017,-73.333740,Type=Disappearance C:>type others.csv 30.685163,-68.137207,Type=Yeti 29.132971,-71.136475,Type=Ship 28.343065,-62.753906,Type=Elvis 27.868217,-68.005371,Type=Goatsucker 29.401320,-66.027832,Type=Ship 37.879536,-69.477539,Type=Elvis 22.705256,-68.192139,Type=Elvis 27.166695,-87.484131,Type=Elvis C:>
0 コメント:
コメントを投稿