C++ 从QFile获取QByteArray并将其写入同一文件
我要编辑一个文件的内容。我正在使用C++ 从QFile获取QByteArray并将其写入同一文件,c++,qt,qbytearray,C++,Qt,Qbytearray,我要编辑一个文件的内容。我正在使用QFile处理文件。现在,我想把它分成小块,比如1024字节。到目前为止,我做到了: 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 eac
QFile
处理文件。现在,我想把它分成小块,比如1024字节。到目前为止,我做到了:
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();
}
}
QFile文件(“~/samplefile”);
long-long-sizeoffice=file.size();
大小=大小/1024///*这是用于循环大小不为1024的情况,因为我想运行循环文件大小/1024,因为在每个循环中我读取1024字节**///
QString contentsToBeErased=“样本”;
QString橡皮擦=内容已删除;
橡皮擦=橡皮擦。填充('*');
int pos=0;//**这是“contentsToBeErased”的位置,以1024字节为单位(每个周期)**//
QByteArray myByteArray;
如果(!file.open(QIODevice::ReadWrite | QIODevice::Text))
返回;
for(long-long i=0;itoUnicode(rohitarray);
if(thisString.contains(contentToBeerBased,Qt::CaseSensitive))
{
int occurrence=thisString.count(contentsToBeErased,Qt::CaseSensitive);
对于(int ii=0;ii使用seek()
访问文件中的特定位置
readLine(n)
应随后读取大小为n的块中的所有字节。第二次打开同一文件可能会造成干扰(可能取决于您的操作系统)。您应使用在ReadWrite
模式下打开的一个文件对象进行读取、写入和查找。使用seek()
用于访问文件中的特定位置
readLine(n)
应随后读取大小为n的块中的所有字节。第二次打开同一文件可能会造成干扰(可能取决于您的操作系统)。您应使用在ReadWrite
模式下打开的一个文件对象进行读取、写入和查找。首先读取文档。
读取直到遇到换行符(“\n”
)或maxSize
字节
在这种特定情况下,您需要和方法。您还需要使用QIODevice::Append | QIODevice::ReadWrite打开QFile
,首先阅读文档。
读取直到遇到换行符(“\n”
)或maxSize
字节
在这种特定情况下,您需要和方法。您还需要使用QIODevice::Append | QIODevice::ReadWrite打开QFile
,检查QIODevice::seek()
检查QIODevice::seek()