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

QFile class

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.9k 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

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

    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shikamarucb
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shikamarucb
            wrote on last edited by
            #5

            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.

            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