ページ 11

低水準出力の問題

Posted: 2011年11月10日(木) 08:40
by HiWorld
しばらく考えているのですがどうしても分からないのでよろしくお願いします。

低水準でopen したファイル(diskfile)にwriteし、ふたたびその書き出したファイルからreadするプログラム、を書いているのですが、writeでファイルに書き出したファイルからreadができません。

手順としては、
openでファイル(diskfile)を作成/オープン。
writeでファイルに書き込む。
それを再びreadによりバッファー(readBuffer)に入れ、標準出力に書き出す。<= ここでのreadでうまくファイルの中身をバッファーに書き込めません。

よろしくお願いします。

main.c

コード:

#include "p242pio.h"
int main (int argc, const char * argv[]) {
   
	int fd,wt,readSize,id;
	void *readBuffer = (void*)calloc(10, 1024);
	
	char strbuff[50]; 

	id = 1;
	sprintf(strbuff, "Write by fd %d", id);
	fd = openDisk("diskfile", 10*1024);
	
	
	wt = writeBlock(fd, id, (void*)strbuff);
		printf("fd %d wrote size of %d byte, data written: %s\n", fd, wt, (char*)strbuff);		
	
	while (1) {
		
		readSize = readBlock(fd, id, readBuffer);
		printf("Read fd %d read size of %d byte ,data read: %s\n", fd,readSize,(char*)readBuffer);
		
		if ( readSize > 0 ){
			printf("readSize = %d\n",readSize);
			write(1, readBuffer, readSize);
		} else if ( readSize == 0 ){
		break;
		}
		
	}
		
	
    return 0;
}
p242pio.h

コード:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int openDisk(char* filename, int nbytes){
	int diskNo;
	
	
	diskNo = open(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
	
	if ( diskNo == -1 ){
        printf("file open/create error\n");
        switch ( errno ){
			case EACCES :
				printf("ファイル名に既存の読み出し専用ファイルを指定したか、");
				printf("またはファイルではなくディレクトリを指定しています。 \n");
				break;
			case EMFILE :
				printf("開いているファイルが多すぎます\n");
				break;
        }
        return 0;
    }else {
		
		printf("nbyte = %d byte\n", nbytes);
		ftruncate(diskNo, nbytes);
	}
	
	
	
	
	return diskNo;
}

int readBlock(int disk, int blocknr, void *block){
	
	int rs;
	int buf[256];
	
	rs = read(disk, block, sizeof(buf));
	
	if ( rs == -1 ){
        printf("read error\n");
	}
	
	
	
	return rs;
	
	
}

int writeBlock(int disk, int blocknr, void *block){
	
	int ws;

	ws = write(disk,block,strlen(block));
	if (ws == -1) {
		printf("write error\n");
	}
	
	
	return	ws;
	
}	


Re: 低水準出力の問題

Posted: 2011年11月10日(木) 10:32
by beatle
実行すると

コード:

nbyte = 10240 byte
fd 3 wrote size of 13 byte, data written: Write by fd 1
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1024 byte ,data read: 
readSize = 1024
Read fd 3 read size of 1011 byte ,data read: 
readSize = 1011
Read fd 3 read size of 0 byte ,data read: 
こうなりますね。この結果をよーくみて原因を考えれば、何か分かると思います。
特に最後から2番目の出力が「1011」なのがアヤシイですよね?

Re: 低水準出力の問題

Posted: 2011年11月10日(木) 17:20
by HiWorld
返信ありがとうございます。

コード:

Read fd 3 read size of 1011 byte ,data read: 
readSize = 1011
Read fd 3 read size of 0 byte ,data read:
もしや1011なのは、readが読み始めるbyteがwriteで書かれた13 byte以降と、言う事でしょうか?

Re: 低水準出力の問題

Posted: 2011年11月10日(木) 19:57
by beatle
1024と1011は13バイトの差なので、当然
「1011なのは、readが読み始めるbyteがwriteで書かれた13 byte以降」
と想像できますよね。そしてこの想像は当たっているのです。

とすると、ファイルの先頭からreadしてあげるようにすればいいわけですね。

Re: 低水準出力の問題

Posted: 2011年11月10日(木) 22:26
by HiWorld
なるほど、ファイルポインタの移動に気づいていませんでした。
無事解決できました。 
ありがとうございました。