2016-04-06 8 views
0

ファイルの内容を編集します。私はQFileを使用してファイルを処理しています。今、私は1024バイトのような小さなチャンクでそれを読んでみたい。これまでのところ、私がやった:QFileからQByteArrayを取得して同じファイルに書き込む

QFile file("~/samplefile"); 

long long sizeoffile = file.size(); 

size = size/1024;   ///*this is for loop size devoid by 1024 because I want to run loop filesize/1024 because in each cycle I read 1024 bytes **/// 
QString contentsToBeErased = "sample"; 

QString eraser = contentsToBeErased; 

eraser = eraser.fill('*'); 

int pos = 0;    ////** This is the position of 'contentsToBeErased' in 1024 bytes(for each cycle) **// 

QByteArray myByteArray; 

if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) 
return; 
for(long long i =0; i<size; i++) 
{ 
    myByteArray = file.readLine(1025); ////**1025 is used bcoz readline reads 1 less bytes**// 

    int sizeArray = 0; 

    QTextCodec *byteArraytoString = QTextCodec::codecForName("UTF-8");  //conevrting bytearray to string 

    QString thisString = byteArraytoString->toUnicode(rohitarray); 

    if(thisString.contains(contenttobeerased, Qt::CaseInsensitive)) 
     { 
      int occurrence = thisString.count(contentsToBeErased,Qt::CaseInsensitive); 
      for(int ii = 0; ii<occurrence; ii++) 
      { 
       pos = thisString.indexOf(contentsToBeErased, pos,Qt::CaseInsensitive); 
       thisString.replace(pos,contentsToBeErased.size(), erase); 
       pos = pos + contentsToBeErased.size() ; 
      } 

      myByteArray = thisString.toUtf8(); 
      sizeArray = myByteArray.length(); 
      QFile file1("~/samplefile"); 
      file1.open(QIODevice::WriteOnly); 
      file1.write(myByteArray); 
      file1.close(); 
      } 
} 

これは最初の試みのために正常に動作しますが、2回目の試みで、私はreadLine(1025);で次の1024バイトの読み込みに失敗しました。最初の1024バイトを再度読み取ります。

私の最初の問題は、readLine();の位置を次の1024バイトにする方法を知らないことです。

write()を使用すると、前のバイト配列が次のバイト配列に置き換えられるため、write()の最初のバイト配列を書き込んだ後に2番目のバイト配列をファイルに書き込む方法がわかりません。では、ファイルの最後にどのように配列を追加できますか?

+0

'QIODevice :: seek()'がチェックされています。 –

答えて

0

seek()を使用して、ファイル内の特定の位置にアクセスします。

readLine(n)は、その後、サイズnのチャンクのすべてのバイトを読み取る必要があります。 2度目に同じファイルを開くと、ここで干渉する可能性があります(おそらくOSによって異なります)。 ReadWriteモードで開くONEファイルオブジェクトを使用して、読み取り、書き込み、検索を行う必要があります。

0

最初にドキュメントをお読みください。 QIODevice::readLine(qint64 maxSize = 0)は改行("\n")またはmaxSizeバイトが発生するまで読み取ります。この特定のケースで

、あなたはpeekseek方法を必要としています。また、QFileQIODevice::Append | QIODevice::ReadWriteで開く必要があります。flags

関連する問題