ファイルの内容を編集します。私は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番目のバイト配列をファイルに書き込む方法がわかりません。では、ファイルの最後にどのように配列を追加できますか?
'QIODevice :: seek()'がチェックされています。 –