ファイル名」、「 > ファイル名」 標準エラー出力は 「 2> ファイル名」 追加出力は 「>>ファイル名」、「2>>ファイル名」"> ファイル名」、「 > ファイル名」 標準エラー出力は 「 2> ファイル名」 追加出力は 「>>ファイル名」、「2>>ファイル名」">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

リダイレクト パイプ 標準入出力プログラム コマンド行引数 関数 system()

Similar presentations


Presentation on theme: "リダイレクト パイプ 標準入出力プログラム コマンド行引数 関数 system()"— Presentation transcript:

1 リダイレクト パイプ 標準入出力プログラム コマンド行引数 関数 system()
コマンドプロンプト操作(2) リダイレクト パイプ 標準入出力プログラム コマンド行引数 関数 system() 今日のポイント リダイレクトとパイプを使いこなそう

2 リダイレクトとは 標準入力、標準出力、標準エラー出力 を "デフォルト"から別のファイルに割り当てる操作
標準入力、標準出力、標準エラー出力 を "デフォルト"から別のファイルに割り当てる操作 "デフォルト"は 「CON」 (コンソール) で、 実際の入力はキーボード、出力はディスプレイ 標準入力は 「 0< ファイル名」、「 < ファイル名」 標準出力は 「 1> ファイル名」、「 > ファイル名」 標準エラー出力は 「 2> ファイル名」 追加出力は 「>>ファイル名」、「2>>ファイル名」

3 リダイレクトの練習(1) Z:\nyumon2>dir > foo.txt
Z:\nyumon2>dir /f 2> err.txt Z:\nyumon2>more < foo.txt Z:\nyumon2>more < err.txt err.txt に エラー出力 foo.txt の 内容を表示 foo.txt に 追加出力 Z:\nyumon2>dir /w >> foo.txt Z:\nyumon2>dir /v 2>> err.txt Z:\nyumon2>more < foo.txt Z:\nyumon2>more < err.txt err.txt に追加 エラー出力 foo.txt の 内容を再表示

4 リダイレクトの練習(2) Z:\nyumon2\hello>hello > hello.txt
Z:\nyumon2>dir > con Z:\nyumon2>dir /f 2> con Z:\nyumon2>more < con > goo.txt abcdefghijklmnopqrstuvwxyz<Enter> Ctrl-z<Enter> Z:\nyumon2>more < goo.txt コンソールに出力 キーボード 入力を goo.txt に保存 ファイルの終わり を表す記号 (教科書p.139 問題14.3) 先週作った、Z:\nyumon2\hello に行き… Z:\nyumon2\hello>hello > hello.txt Z:\nyumon2\hello>more < hello.txt

5 パイプとは ファイル保存をせずに直接、次のプログラムにデータを受け渡す方法 縦棒「|」を使って連結する
標準入力、標準出力のコマンド、プログラムに用いる 並べ替えのコマンド Z:\nyumon1>dir /s /b | sort | more 標準入力/標準出力 標準出力 標準入力/標準出力

6 標準入力・標準出力のコマンド help コマンドで調べてみよう more: ページごとの出力 sort: 並べ替え find: 文字列検索
Z:\nyumon1>dir /s | find "hello" findstr: 高度な文字列検索 Z:\nyumon1>findstr /s "fopen" *.c help コマンドで調べてみよう

7 編集用コマンド notepad (メモ帳) write (ワードパッド) devenv (開発環境)
Z:\nyumon2\hello>notepad hello.c write (ワードパッド) Z:\nyumon2\hello>write Z:\nyumon2\hello>write hello.c devenv (開発環境) Z:\nyumon2\hello>devenv hello.c 新規のとき

8 標準エラー出力を用いたプログラム hello.c を編集し、 hello2.c に名前を変えて保存
→ CL コマンドを用いてコンパイル(cl hello2.c) /* hello world program 2 */ #include <stdio.h> int main(void) { printf("hello world!\n"); fprintf(stderr,"error message!\n"); return 0; } この行を追加 fprintf: ファイルへの出力(教科書p.130) stderr: 標準エラー出力

9 標準エラー出力を用いたプログラムの実行 リダイレクトを用いて実行してみる Z:\nyumon2\hello>hello2 または
 Z:\nyumon2\hello>more < hello2.txt  Z:\nyumon2\hello>more < err2.txt Z:\nyumon2\hello>hello2 > hello2.txt Z:\nyumon2\hello>hello2 2> err2.txt Z:\nyumon2\hello>hello2 >hello2.txt 2>err2.txt

10 標準入力、標準出力を用いたプログラム 以下のプログラムを typ.c と名づけて保存し、コンパイルせよ (自作コマンドのための第1歩)
CL コマンドを用いること /* typ.c: stdin type */ #include <stdio.h> int main(void) { int c; while ((c=fgetc(stdin)) != EOF) fputc(c,stdout); return 0; } stdin から変数c に1文字分入力 ファイルの終わりまで続ける stdout に1文字出力 fgetc(): ファイルから1文字入力 fputc(): ファイルに1文字出力 stdin: 標準入力 EOF: ファイル終端

11 標準入力表示プログラムの実行 Z:\nyumon2>typ < typ.c Z:\nyumon2>typ abc...
123... ^Z Z:\nyumon2>dir | typ type コマンドや more コマンドと 動作を比べてみよ 教科書p.139 問題14.3参照 万一、プログラムが 終われなくなったら、 Ctrl-c で強制終了 Ctrl-z を入力

12 コマンド行引数 プログラムの後にオプションを付けられるようにする (→自作コマンドのための第2歩)
main(void) → main(int argc, char *argv[ ]) int argc: コマンド行の文字列数 char *argv[ ]: コマンド行の文字列配列 例: Z:\nyumon2>mainarg abcde !"#$%&'() argc=4 argv[0]="mainarg" argv[1]="1.2345" argv[2]="abcde" argv[3]="!#$%&'()"

13 コマンド行確認プログラム mainarg.c を作成し、コンパイル・実行せよ
/* mainarg.c: main argument test */ #include <stdio.h> int main(int argc, char *argv[]) { int i; printf("argc = %d\n", argc); for (i = 0; i < argc; i++) { printf("argv[%d] = \"%s\"\n", i, argv[i]); } return 0; エスケープ系列 (教科書p.180)

14 関数 system( ) 関数 system( ) を用いると、プログラム内からコマンドプロンプトのコマンドが利用できる
stdlib.h ヘッダが必要 hello3.c /* hello world program 3 */ #include <stdio.h> #include <stdlib.h> int main(void) { system("cls"); printf("hello world!\n"); return 0; } hello.c に この行を追加 この行も追加 cls について 調べてみよう

15 制御コード 2 \" 2重引用符 \' 単一引用符 \t 水平タブ \\ 円マーク(バックスラッシュ)
教科書 p.180 表A.2 制御コード 2 \" 2重引用符 \' 単一引用符 \t 水平タブ \\ 円マーク(バックスラッシュ) hello4.c /* hello world program 4 */ #include <stdio.h> #include <syslib.h> int main(void) { system("cls"); printf("\\\'hello world!\'\n"); return 0; } この部分を追加

16 文字列を途中で改行するときは必ずダブルクォートで区切る
Officeアプリケーションを開く Excel の実行ファイル(Excel.exe)のありかを確認し、直接実行するプログラムを作る /* Excel exec program */ #include <stdio.h> #include <stdlib.h> int main(void) { system("\"C:\\Program Files\\" "Microsoft Office\\" "Office11\\Excel.exe\""); return 0; } excel2.c 文字列を途中で改行するときは必ずダブルクォートで区切る コマンドにスペースがあるときは 全体をダブルクォート(\")でくくる Word(Winword.exe) や PowerPoint(Powerpnt.exe) を開くプログラムも作ってみよ

17 スキルアップタイム ~コマンド行引数を用いた自作コマンドの作成~
コマンド行で指定した文字の数をカウントする chc.c を完成させよう /* chc.c: char counter */ #include <stdio.h> int main(int argc, char *argv[]) { int c, cnt=0; while ((c=fgetc(stdin)) != EOF) { if ( ) ; } printf(" ", ); return 0;

18 文字数カウンタ chc の実行例 chc のソースファイルで 試した例 Z:\nyumon2>chc i < chc.c
argc = 2 argv[0] = "chc" argv[1] = "i < chc.c" chc のソースファイルで 試した例 Z:\nyumon2>chc i < chc.c i: 10 Z:\nyumon2>chc abc < chc.c a: 6 argc = 2 argv[0] = "chc" argv[1] = "abc < chc.c" 万一、プログラムが 終われなくなったら、 Ctrl-c で強制終了

19 スキルアップタイム (オプション) excel2.c を改良し、ファイル名を指定できるように せよ(excel3.c)
コマンド行でファイル名を指定 excel 実行用文字列に argv[1] を連結 strcat 関数の利用 excel 実行用文字列は初期値で与える argc = 1 のときは連結しない 連結された文字列をsystem関数に渡す


Download ppt "リダイレクト パイプ 標準入出力プログラム コマンド行引数 関数 system()"

Similar presentations


Ads by Google