cプログラミング・演習 プログラミング基礎ii・演習starship, enterprise its 5...

21
Cプログラミング・演習 プログラミング基礎II・演習II 201210296

Upload: others

Post on 17-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

Cプログラミング・演習

プログラミング基礎II・演習II

2012年10月29日

第6回

Page 2: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 2

今後の予定

■12/10

最終講義・演習,期末試験対策

■12/17

期末試験(60分間)

場所:A0542

時間:4時限(15:00~16:00)

■課題の最終提出締切:2012/12/16

これ以降の新規提出は評価されない

Page 3: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

課題の提出方法

■電子メールで提出

■提出先(宛先)

- [email protected]

■メールの題名(件名)

- 後期 第○回課題 学籍番号

例)後期 第6回課題 J123456

●Jは大文字

●ハイフン(-)はいらない

●第○回の○は及び学籍番号は半角数字

■メールの本文

- 課題のプログラムをそのまま記述

2012/10/29 3 Cプログラミング及びプログラミング演習II

Page 4: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 4

■設定変更

- 各プロジェクト毎に以下の設定変更

① [プロジェクト]-[プロジェクト名プロパティ]を選択

② [構成プロパティ]-[リンカ]-[システム]を選択

③ 右ウィンドウの[サブシステム]-[コンソール]に変更

■実行

- [Ctrl]+[F5] (デバッグなしで実行)

プログラムの実行

Page 5: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 5

プログラミングの習得

■プログラミング言語を学ぶ唯一の方法は

「プログラムを書いてみること」

→ 練習課題,提出課題の実行

→ 「習うより慣れろ」というのが経験的な習得方法

Page 6: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

Cプログラミング・演習

プログラミング基礎II・演習II

2012年10月29日

第6回

2012/10/29 6 Cプログラミング及びプログラミング演習II

Page 7: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 7

http://www015.upp.so-net.ne.jp

/class/sec_ex06.html

今日の課題のURL

Page 8: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 8

■ファイル操作の関数

fopen():ファイルを開く(ファイル名の指定)

fclose():ファイルを閉じる

■ファイル入出力関数

fprintf():ファイルへ値を書き込む

fscanf():ファイルから値を読み込む

ファイル操作及び入出力関数

Page 9: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 9

main()

{

FILE *fp;

fp = fopen(“data.txt”, “r”);

…….

…….

fp はファイルポインタ

fopen(“ファイル名”, “モード”)

fopen()とファイルポインタ

Page 10: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 10

「どの」ファイルの「どこ」を読んでいるのかを示すポインタ

Space: The final frontier These are the voyages of the Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations To boldly go where no man has gone before

このファイルの

ここまで読んだ

ポインタ fp で示す

ファイルポインタ

Page 11: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 11

ファイルをオープンし,ファイルポインタを返す

FILE *fp;

fp = fopen(“ファイル名”, “モード”);

モード 動作 ファイルがあるとき ファイルがないとき

r 読み出し専用 正常 エラー NULLを返す

w 書き込み専用 サイズを0にして上書き 新規作成

a 追加書き込み専用 最後に追加する 新規作成

r+ 読み込みと書き込み 正常 エラー NULLを返す

w+ 書き込みと読み込み サイズを0にして上書き 新規作成

a+ 読み込みと追加書き込み 最後に追加する 新規作成

fopen()関数

Page 12: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 12

■ファイルがオープンできなかった場合,ファイルポインタは

NULLを返す

-“r”(ファイル読み込み)でファイルが存在しない場合

-“w”(ファイル書き込み)でファイルが書き込み禁止になっ

ている場合

FILE *fp;

fp = fopen(“data.txt”,”r”);

if ( fp == NULL )

{

printf(“ファイルオープンエラー ¥n”);

exit(1); // プログラム終了

}

fopen()のエラー処理

Page 13: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 13

ファイル操作後にファイルを使わなくなった場合には

fclose() 関数でファイルをクローズ

fclose(fp);

fclose()関数

Page 14: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 14

fprintf()

fprintf( ファイルポインタ, “書き込む内容”, …);

ファイルポインタを指定する以外は printfと同じ

int x=1;

FILE *fp;

fp = fopen(“data.txt”,”w”);

fprintf(fp,“書き込みテスト ¥n”);

fprintf(fp,“変数xの値は%dです”, x);

fclose(fp);

書き込みテスト 変数xの値は1です

data.txt

書き込みモードでオープン

ファイルへの値の書き込み

Page 15: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 15

fscanf()

fscanf(ファイルポインタ, ”書式”, … )

ファイルポインタを指定する以外はfscanfと同じ

int x1, x2, x3;

FILE *fp;

fp = fopen(“data.txt”, “r”);

fscanf(fp,”%d”, &x1);

fscanf(fp,”%d”, &x2);

fscanf(fp,”%d”, &x3);

fclose(fp);

10 15 3 17 19

data.txt

読み込みモードでオープン

x1 = 10 x2 = 15 x3 = 3

ファイルから値の読み込み 1

Page 16: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 16

■ファイルの終わりとして,fscanf()はEOFを返す

int main()

{

int x;

FILE *fp

fp = fopen(“data.txt”,”r”);

if ( fp == NULL )

exit(1);

while ( fscanf(fp, “%d”, &x) != EOF )

{

printf(“x=%d ¥n”, x);

}

fclose(fp);

return 0;

}

10 15 3 17 19

data.txt

EOF

EOF(end of file)

Page 17: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 17

int main()

{

int x;

double y;

FILE *fp

fp = fopen(“data.txt”,”r”);

if ( fp == NULL )

exit(1);

while ( fscanf(fp, “%d %lf”, &x, &y) != EOF )

{

printf(“x=%d , y=%f ¥n”, x, y);

}

fclose(fp);

return 0;

}

170 50.2 181 89.5 175 70.5 172 88.2 171 65.9

data.txt

ファイルと書式を合わせる!!

ファイルから値の読み込み 2

Page 18: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 18

int main()

{

char a[100];

int score;

FILE *fp

fp = fopen(“data.txt”,”r”);

if ( fp == NULL )

exit(1);

while ( fscanf(fp, “%s”, a) != EOF )

{

printf(“%s ¥n”, a);

}

fclose(fp);

return 0;

}

Space The final frontier These are ….(つづく)

表示

Space: The final frontier These are the voyages of the Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations To boldly go where no man has gone before

data.txt

ファイルから値の読み込み 3

Page 19: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 19 Cプログラミング及びプログラミング演習II

■memo.txtを読み込み,以下の仕様を満たすファイresult.html

を出力するプログラムを作成せよ

1.テキストファイルの最初は「<html><body>」で始まる.

2.テキストファイルの最後は「</body></html>」で終わる.

3.テキスト中の最初の文字が大文字である場合(例:Japanese)は,

「<fontcolor=blue>Japanese</font>」に置き換える.

4.テキスト中の数字においては(例:1234),

「<fontcolor=red>1234</font>」に置き換える.

5.それ以外は,そのまま表示する.改行は意識しなくてよい.

■提出期限:2012/11/2(金)24:00

■提出先:[email protected]

提出課題

Page 20: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

2012/10/29 Cプログラミング及びプログラミング演習II 20

Japanese high school pitcher Shohei Otani says he will pursue a career in Major League Baseball instead of turning professional in Japan. …

memo.txt

<html><body> <font color=blue>Japanese</font> high school pitcher <font color=blue>Shohei</font> <font color=blue>Otani</font> says he will pursue …. </body></html>

result.html

提出課題

Page 21: Cプログラミング・演習 プログラミング基礎II・演習Starship, Enterprise Its 5 year mission To explore strange new worlds To seek out new life and new civilizations

課題の提出に関して

2012/10/29 21 Cプログラミング及びプログラミング演習II

■提出先(宛先)

- [email protected]

■メールの題名(件名)

- 後期 第○回課題 学籍番号

例)後期 第6回課題 J123456

●Jは大文字

●ハイフン(-)はいらない

●第○回の○は及び学籍番号は半角数字

■メールの本文

- 課題のプログラムをそのまま記述