write

[機能]

  ハンドルに結合されたファイルへ,指定されたバイト数だけ書き込みます

[形式]
	#include <unistd.h>

	int write(int file, char *buf, unsigned nbyte)
		file  : ファイルハンドル
		buf   : 書き込む内容
		nbyte : データの長さ(バイト)
		
[使用例]

  1. read と write によるファイルへの入出力
    #include <stdio.h>
    #include <sys\types.h>
    #include <sys\stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main()
    {
    	double x1 = 10.5;
    	double x2;
    	long k1 = 21;
    	long k2;
    	int disk;
    	char *buf;
    /*
    		 ディスクへの出力
    */
    	disk = open("data", O_WRONLY|O_CREAT, S_IREAD);
    
    	buf  = (char *)(&x1);
    	write(disk, buf, 8);
    
    	buf = (char *)(&k1);
    	write(disk, buf, 4);
    
    	close(disk);
    /*
    		 ディスクからの入力
    */
    	disk = open("data", O_RDONLY);
    
    	buf  = (char *)(&x2);
    	read(disk, buf, 8);
    
    	buf = (char *)(&k2);
    	read(disk, buf, 4);
    
    	close(disk);
    /*
    		 出力
    */
    	printf("x2 %5.1f k2 %3ld\n", x2, k2);
    
    	return 0;
    }
    
    (出力)
    
    x2  10.5 k2  21
    			
[参照]

read, fwrite, fread

ホームページ 目次 演習解答例目次 付録目次 索引