Program crashing when using buttons to read file content



  • I've wrote a simple program which search in a file ("config.ini" in this case) and upon some use it crashes. I'll copy paste all the program below:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_readButton_clicked();
    void on_writeButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include <QApplication>
    #include <QPushButton>

    int main(int argc, char *argv[])
    {
    QApplication prog(argc, argv);
    MainWindow test;
    test.show();
    return prog.exec();
    }
    @

    @#include <QTCore/QFile>
    #include <QTCore/QTextStream>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_readButton_clicked(){
    QString word=ui->searchLine->text();
    QFile myFile("C:/Users/Administrador/Desktop/Qt/Leitor_texto/config.ini");
    myFile.open(QIODevice::ReadOnly);
    QTextStream textStream(&myFile);
    QString line=textStream.readLine();
    while(!line.startsWith(word)){
    line=textStream.readLine();
    }
    myFile.close();
    word+=" = ";
    if(line.startsWith(word)){
    line.remove(line.indexOf(word),word.size());
    ui->valueFoundText->setPlainText(line);
    ui->statusLabel->setText("Success");
    }
    else{
    ui->valueFoundText->setPlainText("");
    ui->statusLabel->setText("Failed");
    }
    }
    void MainWindow::on_writeButton_clicked(){
    QString word=ui->whatNameLine->text();
    QString value=ui->valueLine->text();
    QFile myFile("C:/Users/Administrador/Desktop/Qt/Leitor_texto/config.ini");
    myFile.open(QIODevice::ReadOnly);
    QTextStream textStream(&myFile);
    QString line=textStream.readAll();
    myFile.close();
    myFile.open(QIODevice::WriteOnly);
    line.replace((line.indexOf(word)+word.size()+3),1,value);
    textStream<<line;
    myFile.close();

    }

    @

    Please help me out.



  • At least I don't see any issue with program. When does it crash ? Does it crash every time ? Can you put some debug statements ? You are reading the line from the file. Can you print them on debug statements and see where the null values are coming ? I sure the your file content is issue here.



  • Sorry for my ignorance but why are you not using the QSettings to read write ini files? have a look "here":http://qt-project.org/doc/qt-5/QSettings.htmlhttp://



  • Dheerendra,

    Since I don't know much about Qt, i have to ask: how I do that? If you show me then we can solve this :D .



  • can you post the content of the file here. also what permissions do you have on that file? what Os are you using. run the program in debug mode and post some debug info here..



  • I've figured out when the crash occurs: it's when I try to find a line which starts with "word", and in the "searchLine" i type something that contains the actual line i'm searching for(the word "cooling" for example) and ADD some other caracter in the end, like "coolingg".

    Below is the "config.ini" file that i'm modifying (this archive is in the same folder as the program, in Desktop.

    I'm using a Windows 7 32 bits to compile the program.

    Also, if selecting "Debug" and click Build is to run in debug mode, then i'm already doing that, but what informations do you guys want to me to post? Point me, and I can provide.

    @#Versao 3.2
    avoid_crossing_perimeters = 0
    bed_size = 180,180
    bed_temperature = 60
    bottom_solid_layers = 3
    bridge_acceleration = 0
    bridge_fan_speed = 100
    bridge_flow_ratio = 1
    bridge_speed = 10
    brim_width = 0
    complete_objects = 0
    cooling = 1
    default_acceleration = 0
    disable_fan_first_layers = 1
    dont_support_bridges = 0
    duplicate_distance = 6
    end_gcode = M104 S0 ; turn off temperature\nM140 S0 ; Desliga Aquecimento da mesa\nG28 X0 Y0 Z0 ; home XYZ axis\nM84 ; disable motors
    external_perimeter_speed = 80%
    external_perimeters_first = 0
    extra_perimeters = 1
    extruder_clearance_height = 20
    extruder_clearance_radius = 20
    extruder_offset = 0x0
    extrusion_axis = E
    extrusion_multiplier = 1.2
    extrusion_width = 0
    fan_always_on = 0
    fan_below_layer_time = 60
    filament_diameter = 3
    fill_angle = 45
    fill_density = 20
    fill_pattern = honeycomb
    first_layer_acceleration = 0
    first_layer_bed_temperature = 60
    first_layer_extrusion_width = 0
    first_layer_height = 100%
    first_layer_speed = 80%
    first_layer_temperature = 190
    g0 = 0
    gap_fill_speed = 20
    gcode_arcs = 0
    gcode_comments = 0
    gcode_flavor = reprap
    infill_acceleration = 0
    infill_every_layers = 1
    infill_extruder = 1
    infill_extrusion_width = 0
    infill_first = 0
    infill_only_where_needed = 0
    infill_speed = 25
    interface_shells = 0
    layer_gcode =
    layer_height = 0.19
    max_fan_speed = 100
    min_fan_speed = 35
    min_print_speed = 8
    min_skirt_length = 0
    notes =
    nozzle_diameter = 0.3
    only_retract_when_crossing_perimeters = 1
    ooze_prevention = 0
    output_filename_format = [input_filename_base].cl
    overhangs = 1
    perimeter_acceleration = 0
    perimeter_extruder = 1
    perimeter_extrusion_width = 0
    perimeter_speed = 25
    perimeters = 2
    post_process =
    print_center = 81.1991,84.9012
    raft_layers = 0
    resolution = 0
    retract_before_travel = 1
    retract_layer_change = 1
    retract_length = 1.2
    retract_length_toolchange = 3
    retract_lift = 0
    retract_restart_extra = 0
    retract_restart_extra_toolchange = 0
    retract_speed = 15
    seam_position = random
    skirt_distance = 2
    skirt_height = 1
    skirts = 0
    slowdown_below_layer_time = 40
    small_perimeter_speed = 15
    solid_fill_pattern = rectilinear
    solid_infill_below_area = 0
    solid_infill_every_layers = 0
    solid_infill_extrusion_width = 0
    solid_infill_speed = 18
    spiral_vase = 0
    standby_temperature_delta = -5
    start_gcode = G28 ; home all axes\nM220 S100 ; modifica a velocidade dos motores
    support_material = 0
    support_material_angle = 50
    support_material_enforce_layers = 0
    support_material_extruder = 1
    support_material_extrusion_width = 200%
    support_material_interface_extruder = 1
    support_material_interface_layers = 4
    support_material_interface_spacing = 0.3
    support_material_interface_speed = 100%
    support_material_pattern = rectilinear
    support_material_spacing = 1.2
    support_material_speed = 25
    support_material_threshold = 30
    temperature = 190
    thin_walls = 1
    threads = 2
    toolchange_gcode =
    top_infill_extrusion_width = 0
    top_solid_infill_speed = 15
    top_solid_layers = 4
    travel_speed = 70
    use_firmware_retraction = 0
    use_relative_e_distances = 0
    vibration_limit = 0
    wipe = 1
    z_offset = 11
    @



  • for debugging you have Debug (in menu) ->Start Debugging -> Start Debugging (F5)
    Or hit F5 to run in the debugger.



  • any way looking to your ini file, my advise is to use QSettings - it will be faster and better also easiest.



  • I suspect that it must be crashing at line#29. Hope your logic is correct. Here you are check is while(!...). It means that you are reading the lines which does not contain the word. So you may be reading some junk line.

    Just do qDebug() << " Line = " << line<<endl like the following and see which line you are hitting the issue.

    @QString line=textStream.readLine();
    while(!line.startsWith(word)){
    qDebug() << " Line = " << line<<endl
    line=textStream.readLine();
    }
    qDebug() << " Line Selected = " << line<<endl
    myFile.close();
    word+=" = ";
    if(line.startsWith(word)){@

    Also you are better off to use QSettings as suggested above.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.