Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Manejando archivos con QT

Estas en el tema de Manejando archivos con QT en el foro de C/C++ en Foros del Web. 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, ...
  #1 (permalink)  
Antiguo 12/01/2015, 16:22
 
Fecha de Ingreso: febrero-2014
Mensajes: 107
Antigüedad: 10 años, 1 mes
Puntos: 0
Manejando archivos con QT

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…

Código C++:
Ver original
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QIODevice>
  4. #include <QFile>
  5. #include <QMessageBox>
  6.  
  7. Widget::Widget(QWidget *parent) :
  8.     QWidget(parent),
  9.     ui(new Ui::Widget)
  10. {
  11.     ui->setupUi(this);
  12.     actualizar();
  13.     ui->quitlink->setEnabled(false);
  14.     connect(ui->buttlink,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
  15. }
  16.  
  17. Widget::~Widget()
  18. {
  19.     delete ui;
  20. }
  21.  
  22.  
  23. void Widget::on_pushButton_clicked()
  24. {
  25.     QByteArray links(ui->link->text().toUtf8());
  26.        if(links.isEmpty()){
  27.            QMessageBox::warning(this,"Advertencia","Campo de direccion web vacio");
  28.        }else{
  29.            links=("\n1.1.1.1 "+links);
  30.            QFile texto("C:/Windows/System32/drivers/etc/hosts");
  31.            texto.open(QIODevice::Append | QIODevice::Text);
  32.            texto.write(links);
  33.            texto.flush();
  34.            texto.close();
  35.        }
  36.     actualizar();
  37. }
  38.  
  39.  
  40.  
  41. void Widget::on_quitlink_clicked()
  42. {
  43.  
  44.     QString links=ui->linklist->currentItem()->text();
  45.     QString var;
  46.     //links="1.1.1.1 " +links;
  47.  
  48.     QFile texto("C:/Windows/System32/drivers/etc/hosts");
  49.  
  50.     texto.open(QIODevice::ReadWrite | QIODevice::Text);
  51.  
  52.     while(!texto.atEnd()){
  53.         var=texto.readLine();
  54.  
  55.         if(var.contains(links)){
  56.             ui->link->setText(QString::number(var.length()));
  57.             if(texto.pos()!=texto.size()){
  58.                 texto.seek(texto.pos()-links.length());
  59.                 for(int i=0;i<=links.length();i++){
  60.                     texto.write("\r");
  61.                     texto.flush();
  62.                 }
  63.             }else{
  64.                 texto.seek(texto.pos()-links.length()-1);
  65.                 for(int i=0;i<=links.length();i++){
  66.                     texto.write("\r");
  67.                     texto.flush();
  68.                 }
  69.             }
  70.         }
  71.  
  72.     }
  73.  
  74.     texto.close();
  75.     ui->quitlink->setEnabled(false);
  76.     actualizar();
  77. }
  78.  
  79.  
  80.  
  81. void Widget::on_linklist_clicked(const QModelIndex &index)
  82. {
  83.     ui->quitlink->setEnabled(true);
  84. }
  85.  
  86.  
  87.  
  88. void Widget::actualizar(){
  89.     ui->linklist->clear();
  90.  
  91.     QFile texto("C:/Windows/System32/drivers/etc/hosts");
  92.     QString var;
  93.     texto.open(QIODevice::ReadOnly | QIODevice::Text);
  94.     while(!texto.atEnd()){
  95.         var = texto.readLine();
  96.         texto.flush();
  97.         if(var.at(0)!= '#'){
  98.             if(!var.isNull())
  99.                 ui->linklist->addItem(var);
  100.         }
  101.     }
  102.     texto.close();
  103. }

Etiquetas: clase
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:07.