I am new in qt-4 c++98.I am told to do using file data not any stream data type.I am loading the text file saved in local directory in qt form on **load** button click.
Here is main.cpp code
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Here is MainWindow.h code
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <stdio.h>
#include <stdlib.h>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
public slots:
void Load();
void Update();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Here is MainWindow.cpp code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
int i, n=2;
char str[50],Name[50],Class[50],Grade[50],var1[50],var2[50];
FILE *fptr;
int count=0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(Load()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(Update()));
}
void MainWindow::Load(){
fptr = fopen("C:\\Users\\hp\\Desktop\\aa\\sample.conf.txt", "r+");
if (fptr == NULL)
{
printf("Could not open file");
}
for(int i=0;i<50;i++) {
if(EOF==fscanf(fptr,"%s",var1) )
{
break;
}
if(EOF==fscanf(fptr,"%s",var2) )
{
break;
}
if( strcmp(var2,"Name") == 0)
{
sprintf(Name,"%s", var1);
ui->lineEdit_1->setText(QString::fromStdString(Name));
}
if( strcmp(var2,"Class") == 0)
{
sprintf(Class,"%s", var1);
ui->lineEdit_2->setText(QString::fromStdString(Class));
}
if( strcmp(var2,"Grade") == 0)
{
sprintf(Grade,"%s", var1);
ui->lineEdit_3->setText(QString::fromStdString(Grade));
}
fclose(fptr);
}
}
}
void MainWindow::Update(){
fptr = fopen("C:\\Users\\hp\\Desktop\\aa\\sample.conf.txt", "r+");
strcpy(Name,ui->lineEdit_1->setText(QString::fromStdString(Name)) );
strcpy(Class,ui->lineEdit_1->setText(QString::fromStdString(Class)));
strcpy(Grade,ui->lineEdit_1->setText(QString::fromStdString(Grade)));
if (fptr == NULL)
{
printf("Could not open file");
}
for(int i=0;i<4;i++) {
if(EOF==fscanf(fptr,"%s",var1) )
{
break;
}
if(EOF==fscanf(fptr,"%s",var2) )
{
break;
}
if( strcmp(var2,"Name") == 0)
{
fprintf(fptr ,"%s\n",Name);
}
if( strcmp(var2,"Class") == 0)
{
fprintf(fptr ,"%s\n",Class);
}
if( strcmp(var2,"Grade") == 0)
{
fprintf(fptr ,"%s\n",Grade)/;
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
Here is sample.conf.txt
Quote:
Hello. Name
One. Class
A+. Grade
On pressing Load button it shows
Quote:
Name Hello
Class One
Grade. A+
On pressing Update button by addind random values i.e.,
Quote:
Name AA
Class BB
Grade. CC
Here is my updated sample.conf.txt
Quote:
Hello. NameAA
One. ClassBB
A+. GradeCC
it should be like this Model
Quote:
Name AA
Class BB
Grade. CC
How i can make right update like Model?
What I have tried:
Here is the image file of UI
[enter image description here][1]
[1]: https://i.stack.imgur.com/dkxGR.jpg