練習問題 ファイル入出力
練習問題 1 ファイルへのデータ出力 配列 a[ ] の値をファイル data.txt に出力するプログラムを作成しなさい #include <stdio.h> #include <stdlib.h> /* srand(), rand() */ #include <time.h> /* time() */ int main(void) { int i; double a[5]; char filename[21] = "data.txt";???? *fp; /* ファイルポインタの宣言 */ /* ファイルへデータを書き込む */ for(i = 0; i < 5; i++)???????(fp, "%.2lf n", a[i]); fclose(fp); /* 乱数発生 (0~1000 までの乱数を 100 で割った値 ) */ srand((unsigned int)time(null)); for(i = 0; i < 5; i++){ a[i] = rand() % 1000 / 100.0; /* ファイルを書き込みモードでオープンする */ if((fp =?????(filename,????)) == NULL){ printf(" ファイルオープンに失敗しました. n"); data.txt 1.23 2.45 3.56 4.78 5.91 data.txt ファイルができるので, メモ帳などで確認してみる
練習問題 2 ファイルからのデータ入力 練習問題 1 で作成されたファイル data.txt からデータを読み込んで配列 a[ ] に格納したあと, 配列 a[ ] の合計を計算するプログラムを作成しなさい #include <stdio.h> #define N 5 int main(void) { int i, n; double a[n], sum; char filename[21] = "data.txt"; FILE *fp; /* ファイルを読み込みモードでオープンし */ /* ファイルポインタ fp に接続 */ if((?? = fopen(filename,????)) == NULL){ printf(" ファイルオープンに失敗しました. n"); /* ファイルの最後まで (EOF まで ) 読み込む */ i = 0; while(??????(??, "%lf", &a[i])!= EOF){ i++; n = i; sum = 0.0; for(i = 0; i < n; i++){ printf("a[%d] = %.2lf n", i, a[i]); sum += a[i]; printf(" 合計 :%.2lf n", sum);??????(fp); /* ファイルをクローズ */ data.txt 1.23 2.45 3.56 4.78 5.91 読み込む a[0] a[1] a[2] a[3] a[4] 1.23 2.45 3.56 4.78 5.91
練習問題 3 ファイルからの文字列入力 英文ファイル abstract.txt に含まれる 'a' および 'A' の文字数を数えるプログラムを作成しなさい #include <stdio.h> #define N 256 int main(void) { int i, counta; char buf[n]; char fnam[21] = "abstract.txt"; FILE *fp; /* ファイルを読み込みモードでオープン */ if((fp = fopen(fnam,????)) == NULL){ printf("[%s]: オープンに失敗 n", fnam); counta = 0; /* ファイルから 1 行読み込むのを NULL まで繰り返す */ while(?????(buf, N,??)!= NULL){ i = 0; while(buf[i]!=????){ /* 文字列の最後まで */ /* 文字列の文字が 'a' もしくは 'A' であるかどうか */ if(buf[i] ==??? buf[i] ==???){????????? /* そうであれば counta を +1*/???? /* 次の文字を見る */ fclose(fp); printf(" a および A は "); printf("%d 文字含まれる. n", counta);
練習問題 3 ファイルからの文字列入力 ( 解説 ) abstract.txt A new method is proposed for detection of the temporal changes using 3-dimensional segmentation. The method is a kind of clustering methods for temporal changes.[ 改行 ] In the method, multi-temporal images form a image block in 3-dimensional space; x-y plane and time axis. [ 改行 ] The image block is firstly divided into spatially uniform sub-blocks by applying binary division process. [ 改行 ] The division rule is based on the statistical t-test using Mahalanobis distance between spatial coefficient vectors of a local regression model fitted to neighboring sub-blocks to be divided. [ 改行 ] The divided sub-blocks are, then, merged into clusters by using a clustering technique. [ 改行 ] The block based processing such as the spatial segmentation technique is very effective in reduction of apparent changes due to noise. [ 改行 ] Temporal change is detected as a boundary perpendicular to the time axis in the segmentation result. [ 改行 ] The proposed method is successfully applied to actual multitemporal and multispectral LANDSAT/TM images. [EOF] ファイルから最大 255 文字読み込み, に格納 char = ~ " A new method is ~ changes. " fgets によって [ 改行 ] まで読み込まれる [0] A n e w c h a n すなわち [1] [2] [3] [4] [5] [6] [7] [8] g m e t ~ [90] [91] [92] [93] [94] [95] [96] [97] [98] e s. 0 実際には [ 改行 ] のコードが入る
練習問題 3 ファイルからの文字列入力 ( 解説 ) 文字列 に格納されている文字を 1 文字ずつ検索していく [0] [1] [2] [3] [4] [5] [6] [7] [8] [90] [91] [92] [93] [94] [95] [96] [97] [98] A n e w m e t ~ c h a n g e s. 0 'a' or 'A'? 真 counta を +1 して次の文字を見る [0] [1] [2] [3] [4] [5] [6] [7] [8] [90] [91] [92] [93] [94] [95] [96] [97] [98] A n e w m e t ~ c h a n g e s. 0 'a' or 'A'? 偽 何もしないで次の文字を見る [0] [1] [2] [3] [4] [5] [6] [7] [8] [90] [91] [92] [93] [94] [95] [96] [97] [98] A n e w m e t ~ c h a n g e s. 0 ' 0' であれば, 終了
練習問題 3 ファイルからの文字列入力 ( 解説 ) abstract.txt A new method is proposed for detection of the temporal changes using 3-dimensional segmentation. The method is a kind of clustering methods for temporal changes.[ 改行 ] In the method, multi-temporal images form a image block in 3-dimensional space; x-y plane and time axis. [ 改行 ] The image block is firstly divided into spatially uniform sub-blocks by applying binary division process. [ 改行 ] The division rule is based on the statistical t-test using Mahalanobis distance between spatial coefficient vectors of a local regression model fitted to neighboring sub-blocks to be divided. [ 改行 ] The divided sub-blocks are, then, merged into clusters by using a clustering technique. [ 改行 ] The block based processing such as the spatial segmentation technique is very effective in reduction of apparent changes due to noise. [ 改行 ] Temporal change is detected as a boundary perpendicular to the time axis in the segmentation result. [ 改行 ] The proposed method is successfully applied to actual multitemporal and multispectral LANDSAT/TM images. [EOF] EOF( ファイルの終わり ) まで読み込み 検索の処理を繰り返す