ふつりな

バイト単位の入出力

さて、システムコールのread/writeではストリームを指し示すためにint型のファイルディスクリプタを使用しましたが、標準入出力関数ではFILE型の変数を使用します。 FILE *fopen(const char *path, const char *mode); ファイルにつながるストリームを開く i…

5.8 練習問題の解答

P100 練習問題。 1. 本章で作ったcatコマンドを改造して、コマンドライン引数でファイル名が渡されなかったら標準入力を読むようにしなさい。 do_cat()をオーバーロードして、 static void do_cat( const char *path ); static void do_cat( int fd );として…

catコマンドを作る

P86 掲載されているソースを見ないで書いてみる。 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define BUF_SIZE 1024 void do_cat( const char *pszFileName ) { int fd = open( pszFileName, O_RDONLY ); if (fd > 0) { char buf[BUF_SIZE]; int b</fcntl.h></sys/stat.h></sys/types.h></unistd.h></stdio.h>…

C言語の変数宣言

P18 takatoshi@sarge:~/projects/futuu_no_linux$ gcc -dumpversion 3.3.6で、 takatoshi@sarge:~/projects/futuu_no_linux$ cat args.c #include "stdio.h" int main( int argc, char *argv[] ) { printf("argc=%d?n", argc); for ( int i = 0; i < argc; +…