コマンドプロンプト操作2

コマンドプロンプト操作(2)





リダイレクト
パイプ
標準入出力プログラム
コマンド行引数
関数 system()
今日のポイント
リダイレクトとパイプを使いこなそう
リダイレクトとは

標準入力、標準出力、標準エラー出力 を
"デフォルト"から別のファイルに割り当てる操作

"デフォルト"は 「CON」 (コンソール) で、
実際の入力はキーボード、出力はディスプレイ

標準入力は 「 0< ファイル名」、「 < ファイル名」

標準出力は 「 1> ファイル名」、「 > ファイル名」

標準エラー出力は 「 2> ファイル名」

追加出力は 「>>ファイル名」、「2>>ファイル名」
リダイレクトの練習(1)
Z:\nyumon2>dir > foo.txt
Z:\nyumon2>dir /f 2> err.txt
Z:\nyumon2>more < foo.txt
Z:\nyumon2>more < err.txt
Z:\nyumon2>dir /w
Z:\nyumon2>dir /v
Z:\nyumon2>more <
Z:\nyumon2>more <
>> foo.txt
2>> err.txt
foo.txt
err.txt
foo.txt に出力
err.txt に
エラー出力
foo.txt の
内容を表示
foo.txt に
追加出力
err.txt に追加
エラー出力
foo.txt の
内容を再表示
リダイレクトの練習(2)
Z:\nyumon2>dir > con
コンソールに出力
Z:\nyumon2>dir /f 2> con
キーボード
Z:\nyumon2>more < con > goo.txt
入力を
goo.txt
abcdefghijklmnopqrstuvwxyz<Enter>
に保存
Ctrl-z<Enter>
ファイルの終わり
を表す記号
Z:\nyumon2>more < goo.txt
(教科書p.139 問題14.3)
先週作った、Z:\nyumon2\hello に行き…
Z:\nyumon2\hello>hello > hello.txt
Z:\nyumon2\hello>more < hello.txt
パイプとは

ファイル保存をせずに直接、次のプログラム
にデータを受け渡す方法

縦棒「|」を使って連結する

標準入力、標準出力のコマンド、プログラム
に用いる
並べ替えのコマンド
Z:\nyumon1>dir /s /b | sort | more
標準出力
標準入力/
標準出力
標準入力/
標準出力
標準入力・標準出力のコマンド

more: ページごとの出力

sort: 並べ替え

find: 文字列検索
Z:\nyumon1>dir /s | find "hello"

findstr: 高度な文字列検索
Z:\nyumon1>findstr /s "fopen" *.c
help コマンドで調べてみよう
編集用コマンド

notepad (メモ帳)
Z:\nyumon2\hello>notepad hello.c

write (ワードパッド)
Z:\nyumon2\hello>write
新規のとき
Z:\nyumon2\hello>write hello.c

devenv (開発環境)
Z:\nyumon2\hello>devenv hello.c
Z:\nyumon2\hello>hello.c
標準エラー出力を用いたプログラム
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: 標準エラー出力
標準エラー出力を用いたプログラムの実行

リダイレクトを用いて実行してみる
Z:\nyumon2\hello>hello2
Z:\nyumon2\hello>hello2 > hello2.txt
Z:\nyumon2\hello>hello2 2> err2.txt
または
Z:\nyumon2\hello>hello2 >hello2.txt 2>err2.txt
Z:\nyumon2\hello>more < hello2.txt
Z:\nyumon2\hello>more < err2.txt
標準入力、標準出力を用いたプログラム

以下のプログラムを typ.c と名づけて保存し、コンパイルせよ
(自作コマンドのための第1歩)
/* typ.c: stdin type */
#include <stdio.h>
CL コマンドを
用いること
int main(void)
{
stdin から変数c に1文字分入力
int c;
while ((c=fgetc(stdin)) != EOF) fputc(c,stdout);
return 0;
}
stdout に1文字出力
ファイルの終わりまで続ける
fgetc(): ファイルから1文字入力
fputc(): ファイルに1文字出力
stdin: 標準入力
EOF: ファイル終端
標準入力表示プログラムの実行
Z:\nyumon2>typ < typ.c
...
type コマンドや
more コマンドと
動作を比べてみよ
Z:\nyumon2>typ
abc...
abc...
教科書p.139 問題14.3参照
123...
123...
万一、プログラムが
Ctrl-z を入力
^Z
終われなくなったら、
Ctrl-c で強制終了
Z:\nyumon2>dir | typ
コマンド行引数




プログラムの後にオプションを付けられるようにする
(→自作コマンドのための第2歩)
main(void) → main(int argc, char *argv[ ])
int argc: コマンド行の文字列数
char *argv[ ]: コマンド行の文字列配列
例: Z:\nyumon2>mainarg 1.2345 abcde !"#$%&'()
argc=4
argv[0]="mainarg"
argv[1]="1.2345"
argv[2]="abcde"
argv[3]="!#$%&'()"
コマンド行確認プログラム

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)
}
関数 system( )


関数 system( ) を用いると、プログラム内から
コマンドプロンプトのコマンドが利用できる
stdlib.h ヘッダが必要
hello3.c
/* hello world program 3 */
#include <stdio.h>
hello.c に
#include <stdlib.h>
この行を追加
int main(void)
{
この行も追加
system("cls");
printf("hello world!\n");
cls について
return 0;
調べてみよう
}
制御コード 2


\" 2重引用符
\t 水平タブ
教科書
p.180 表A.2
\' 単一引用符
\\ 円マーク(バックスラッシュ)
/* hello world program 4 */
hello4.
c
#include <stdio.h>
#include <syslib.h>
int main(void)
{
system("cls");
printf("\\\'hello world!\'\n");
return 0;
}
この部分を追加
Officeアプリケーションを開く

Excel の実行ファイル(Excel.exe)のありか
を確認し、直接実行するプログラムを作る
/* Excel exec program */
excel2.c
#include <stdio.h>
#include <stdlib.h>
文字列を途中で改行
int main(void)
するときは必ずダブ
{
ルクォートで区切る
system("\"C:\\Program Files\\"
"Microsoft Office\\"
"Office11\\Excel.exe\"");
return 0;
コマンドにスペースがあるときは
}
全体をダブルクォート(\")でくくる
Word(Winword.exe) や PowerPoint(Powerpnt.exe)
を開くプログラムも作ってみよ
スキルアップタイム
~コマンド行引数を用いた自作コマンドの作成~

コマンド行で指定した文字の数をカウントする 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;
}
文字数カウンタ chc の実行例
chc のソースファイルで
試した例
argc = 2
argv[0] = "chc"
argv[1] = "i < chc.c"
Z:\nyumon2>chc i < chc.c
i: 10
Z:\nyumon2>chc abc < chc.c
a: 6
万一、プログラムが
終われなくなったら、
Ctrl-c で強制終了
argc = 2
argv[0] = "chc"
argv[1] = "abc < chc.c"
スキルアップタイム (オプション)
excel2.c を改良し、ファイル名を指定できるように
せよ(excel3.c)
 コマンド行でファイル名を指定
 excel 実行用文字列に argv[1] を連結




strcat 関数の利用
excel 実行用文字列は初期値で与える
argc = 1 のときは連結しない
連結された文字列をsystem関数に渡す