Clase QFile
-
Hola a todos, tengo un problema, y es que quiero editar un archivo del sistema; el problema es que deseo eliminar una linea del archivo, pero no se como hacerlo correctamente; tengo el siguiente codigo y al borrar un "link" se elimina del archivo, pero el tamaño del archivo sigue igual; visualmente se ve que no ya no queda rastro de la linea pero agrega otros caracteres invisibles...
@#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();
}
@