Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Spanish
  4. Clase QFile
Qt 6.11 is out! See what's new in the release blog

Clase QFile

Scheduled Pinned Locked Moved Spanish
1 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shikamarucb
    wrote on last edited by
    #1

    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();
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved