QFile class
-
Hi all, I have a problem, and I want to edit a file system; the problem is that I want to delete a line of the file, but not how to do it properly; I have the following code and delete a "link" is removed from the file, but the file size remains the same; Visually is not no trace of the line but adds other invisible characters ...
translated by google
@#include "widget.h"
#include "ui_widget.h"
#include <QIODevice>
#include <QFile>
#include <QMessageBox>Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
actualizar();
ui->quitlink->setEnabled(false);
connect(ui->buttlink,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
}Widget::~Widget()
{
delete ui;
}void Widget::on_pushButton_clicked()
{
QByteArray links(ui->link->text().toUtf8());
if(links.isEmpty()){
QMessageBox::warning(this,"Advertencia","Campo de direccion web vacio");
}else{
links=("\n1.1.1.1 "+links);
QFile texto("C:/Windows/System32/drivers/etc/hosts");
texto.open(QIODevice::Append | QIODevice::Text);
texto.write(links);
texto.flush();
texto.close();
}
actualizar();
}void Widget::on_quitlink_clicked()
{QString links=ui->linklist->currentItem()->text(); QString var; //links="1.1.1.1 " +links; QFile texto("C:/Windows/System32/drivers/etc/hosts"); texto.open(QIODevice::ReadWrite | QIODevice::Text); while(!texto.atEnd()){ var=texto.readLine(); if(var.contains(links)){ ui->link->setText(QString::number(var.length())); if(texto.pos()!=texto.size()){ texto.seek(texto.pos()-links.length()); for(int i=0;i<=links.length();i++){ texto.write("\r"); texto.flush(); } }else{ texto.seek(texto.pos()-links.length()-1); for(int i=0;i<=links.length();i++){ texto.write("\r"); texto.flush(); } } } } texto.close(); ui->quitlink->setEnabled(false); actualizar();
}
void Widget::on_linklist_clicked(const QModelIndex &index)
{
ui->quitlink->setEnabled(true);
}void Widget::actualizar(){
ui->linklist->clear();QFile texto("C:/Windows/System32/drivers/etc/hosts"); QString var; texto.open(QIODevice::ReadOnly | QIODevice::Text); while(!texto.atEnd()){ var = texto.readLine(); texto.flush(); if(var.at(0)!= '#'){ if(!var.isNull()) ui->linklist->addItem(var); } } texto.close();
}
@ -
Hi,
Removing some chars from a file doesn't necessarily modify its on disk size. A file is stored in one or more cluster which represent the minimum size of a file
-
Hola de nuevo, se supone que los archivos de texto representan un byte por caracter y que a medida que yo quito o pongo caracteres,el archivo cambia su tamaño, esta es la unica manera que yo he encontrado para recorrer el archivo y eliminar datos, trata de compilar mi codigo y veras que al quedar un sola linea en el archivo, aparecen caracteres "fantasma" que no permiten un desplazamiento adecuado dentro del archivo.
translated by google
-
The easiest approach I guess can be:
- create (open) temporary file for writing.
- open original file
- in a loop until the end of the file:
- read line from original file,
- write line you want to keep to temporary file
- rename temporary file
Hope this helps,
Alex -
Hello again, it is assumed that the text files represent one byte per character and as I remove or put characters, the file changes its size, this is the only way I've found to traverse the file and delete data; try to compile my code and see that to stay a single line in the file, characters "ghost" that do not adequately offset within the file appear.