1

FileHandleクラスにはfileHandleWithStandardOutputメソッドがあります。ドキュメントによると、「従来、これはプログラムからデータのストリームを受信する端末デバイスです。」

私がやりたいのは、128バイトごとにファイルを読み取り、fileHandleWithStandardOutputメソッドを使用して端末に表示することです。

これは、128バイトごとにどのように読み取っているのかを示すコードフラグメントです。

i = 0;
while((i + kBufSize) <= sourceFileSize){
        [inFile seekToFileOffset: i];
        buffer = [inFile readDataOfLength: kBufSize];
        [outFile seekToEndOfFile];
        [outFile writeData: buffer];
        i += kBufSize + 1;        
    }

//Get the remaining bytes...
[inFile seekToFileOffset: i ];

buffer = [inFile readDataOfLength: ([[attr objectForKey: NSFileSize]intValue] - i)];
[outFile seekToEndOfFile];
[outFile writeData: buffer];

kBufSizeは、128に等しいプリプロセッサです。


私の答え:

outFileをfileHandleWithStandardOutput..の戻りNSFileHandleに設定します。

以前に試しましたが、うまくいきませんでした。そして今はうまくいきました。他に何かがあるか、何かが干渉している可能性があります。とにかく私は今答えを得ました。

4

2 に答える 2

2

FileHandleからの読み取りまたはFileHandleへの書き込みのたびにシークする必要はありません。コードは次のように簡略化できます。

NSData *buffer;

NSFileHandle *outFile = [NSFileHandle fileHandleWithStandardOutput];

do {
    buffer = [inFile readDataOfLength: kBufSize];
    [outFile writeData:buffer];
} while ([buffer length] > 0);

128バイトのチャンクで読み取っている理由はわかりませんが、それが必要でない場合は、ループを削除して、代わりに次のようにすることができます(入力ファイルがNSDataの最大値を超えるほど大きくない場合)物体):

NSFileHandle *outFile = [NSFileHandle fileHandleWithStandardOutput];
buffer = [inFile readDataToEndOfFile];
[outFile writeData:buffer];
于 2011-12-16T03:32:17.670 に答える
0

次の手順を実行するだけで、目的を達成できます。

while ( ( buffer = [inFile readDataOfLength:kBuffSize] ).length !=0 ){
    [standardOutPut writeData:buffer];
}
于 2014-05-28T08:14:37.720 に答える