PDA

View Full Version : Need help asap! Filestream



Patch
5th May 2016, 14:23
Hi! I'm so new to qt and i don't know how to do a filestream. I am creating a login form-filestream based. So i need to create a login form which has of course username and password. First what i did was to use SQLITE as a database and it worked. But the problem is. I have to use filestream that would end up making a new .txt file where the username and passwords would be inputed. I don't know the proper codes to be used in making a filestream from qt and i don't know where in my main code i should put it. i badly need help. The codes that are written there are for sqlite base but i need to change it to a filestream type that would be based from a .txt file. Here's my code:



#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QPixmap>
#include <QFont>
#include <QSplashScreen>
#include <QTimer>

Login::Login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
QPixmap pix("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/images/background.jpg");
ui->background->setPixmap(pix);

mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/sqlite/users.db");

if(!mydb.open())
ui->status->setText("Failed to open the database");

else
ui->status->setText("Connected Succesfuly");
}

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

void Login::on_pushButton_login_clicked()
{


QString name,password;
name=ui->lineEdit_Username->text();
password=ui->lineEdit_Password->text();

if(!mydb.isOpen()){

qDebug()<<"Failed to open the database";
return;
}

QSqlQuery qry;

if(qry.exec("select * from users.db11923 where name='"+name+"'and password='"+password+"'"))
{
int count=0;
while(qry.next())
{
count++;
}
if(count==1)
{
QMessageBox::information(this,"Login","Username and password is correct ");
hide();
secDiag = new SecDiag(this);
secDiag->show();

}

if(count>1)
{
QMessageBox::information(this,"Login","Username and password is incorrect");
}
if(count<1)
{
QMessageBox::information(this,"Login","Username and password is incorrect");
}


}




}

anda_skoa
5th May 2016, 15:37
if you want to write into a text file, just use QFile to open the file, pass it to a QTextStream instance and write using that stream.

Cheers,
_

Patch
5th May 2016, 17:22
I got the command for the QFile. my text file for example looks like this: "Patch"|"Cool" . i got it by exporting the database from SQLITE Manager. Where Patch should represent the username then Cool would act as the password. My problem is that how can I declare that the term "Patch" is the username as well as for the password.

d_stranz
5th May 2016, 18:14
Use the QTextStream::operator>>() to read the file into a QString. Use QString::split() with '|' as the split character to split the string into a QStringList. This should have a size of 2. Use QStringList::at() with the index 0 to extract the user name into your username QString variable, use the same method with index 1 to get the password into its QString variable. If you need to remove the quotes, you can use QString::remove().

anda_skoa
6th May 2016, 09:28
My problem is that how can I declare that the term "Patch" is the username as well as for the password.
This is a text file, you don't need to declare anything.
You interpret the data according to whatever your format says when reading.


Use the QTextStream::operator>>() to read the file into a QString.

I would suggest using QTextStream::readLine() instead.
The stream operator only reads until the first white space, content can often contain these (here the password could, for example).

Cheers,
_

Patch
6th May 2016, 18:19
I got this one

void Login::on_pushButton_login_clicked()
{

char str[619];//and his name is reyy misterio
QString name,password;
string infile_name,infile_password;
string infile_fullname;
name=ui->lineEdit_Username->text();
password=ui->lineEdit_Password->text();

//the login source codes ;)
ifstream infile("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/sqlite/users4.txt");
infile.getline(str,619,';');
infile_name = str;
infile.getline(str,619,';');
infile_password = str;
infile.getline(str,619,'\n');
infile_fullname = str;



while(!infile.eof()){



if(infile_name == name.toStdString() && infile_password == password.toStdString())
{

QMessageBox::information(this,"Login","Username and password is correct ");
hide();
secDiag = new SecDiag(this);
secDiag->show();

break; //This will display the next dialog
}

else //this will be used as a false statement


QMessageBox::information(this,"Login","Username and password is incorrect");



infile.getline(str,619,';');
infile_name = str;
infile.getline(str,619,';');
infile_password = str;
infile.getline(str,619,'\n');
infile_fullname = str;



it is actually working but there is one problem. The value for example the "infile_name" never changes. it stays as "name" and if it is compared to the Qstring variable "name" it is always false. Know a solution?