Hey, so i have an app which downloads some txt, which i then save into a app created txt file.
But the issuse is, the code is working when i have a specific file path, but wont work when i change it.
I'm a newb so keep that in mind please
the code
i "downloaded" this QT class for getting the file
downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
{
Q_OBJECT
public:
explicit downloader
(QObject *parent
= 0);
void Do_download();
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
#endif // DOWNLOADER_H
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
class downloader : public QObject
{
Q_OBJECT
public:
explicit downloader(QObject *parent = 0);
void Do_download();
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
#endif // DOWNLOADER_H
To copy to clipboard, switch view to plain text mode
downloader.cpp
#include "downloader.h"
#include "strings.h"
downloader
::downloader(QObject *parent
) :{
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager
->get
(QNetworkRequest
(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
msgBox.setText("Error while reading");
msgBox.exec();
}
else
{
{
file->write(data);
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
#include "downloader.h"
#include "strings.h"
downloader::downloader(QObject *parent) :
QObject(parent)
{
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
QMessageBox msgBox;
msgBox.setText("Error while reading");
msgBox.exec();
}
else
{
QFile *file = new QFile(strings::filePathQt);
QByteArray data = reply->readAll();
if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
{
file->write(data);
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
To copy to clipboard, switch view to plain text mode
then im using this code to parse the file
parser.h
#ifndef PARSER_H
#define PARSER_H
#include <QMessageBox>
#include <QStringList>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <algorithm>
class parser
{
public:
explicit parser();
std::list<std::string> curr_list;
void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
int read_line(std::map<std::string,double> &my_map);
void init_list();
private:
};
#endif // PARSER_H
#ifndef PARSER_H
#define PARSER_H
#include <QMessageBox>
#include <QStringList>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <algorithm>
class parser
{
public:
explicit parser();
QStringList currency_list;
std::list<std::string> curr_list;
void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
int read_line(std::map<std::string,double> &my_map);
void init_list();
private:
};
#endif // PARSER_H
To copy to clipboard, switch view to plain text mode
parser.cpp
#include "parser.h"
#include "calculator.h"
#include "strings.h"
parser::parser()
{
init_list();
}
void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
{
double curr;
std::string temp;
temp = line.substr(53,8);
std::replace(temp.begin(),temp.end(),',','.');
curr = std::stod(temp,NULL);
my_map.insert(std::make_pair(curr_string,curr));
}
int parser::read_line(std::map<std::string,double> &my_map)
{
std::list<std::string>::iterator iter;
std::string linija;
std::ifstream file_("C:/Qt/test/downloaded.txt");
if(file_.fail())
{
msgBox.setText("File opening failed");
msgBox.exec();
}
else if(file_.is_open())
{
while(std::getline(file_,linija))
{
for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
{
if(linija.find(*iter) != std::string::npos)
process_line(linija, my_map, *iter);
}
}
file_.close();
}
// if(!file.exists())
// {
//
// QMessageBox msgBox;
// msgBox.setText("There is no such file");
// msgBox.exec();
// return 1;
// }
// if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
// {
//
// QMessageBox msgBox;
// msgBox.setText("Error while opening file");
// msgBox.exec();
// return 1;
// }
// QTextStream in_stream(&file);
//std::string line=in_stream.readLine().toStdString();
//
//while(true)
// {
// process_line(line, my_map);
// line = in_stream.readLine().toStdString();
// if(in_stream.atEnd()) break;
// }
//this was the original QT code i used, but then i had to switch to std containers, so i had to make some twitches.
return 0;
}
#include "parser.h"
#include "calculator.h"
#include "strings.h"
parser::parser()
{
init_list();
}
void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
{
double curr;
std::string temp;
temp = line.substr(53,8);
std::replace(temp.begin(),temp.end(),',','.');
curr = std::stod(temp,NULL);
my_map.insert(std::make_pair(curr_string,curr));
}
int parser::read_line(std::map<std::string,double> &my_map)
{
std::list<std::string>::iterator iter;
std::string linija;
std::ifstream file_("C:/Qt/test/downloaded.txt");
if(file_.fail())
{
QMessageBox msgBox;
msgBox.setText("File opening failed");
msgBox.exec();
}
else if(file_.is_open())
{
while(std::getline(file_,linija))
{
for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
{
if(linija.find(*iter) != std::string::npos)
process_line(linija, my_map, *iter);
}
}
file_.close();
}
// if(!file.exists())
// {
//
// QMessageBox msgBox;
// msgBox.setText("There is no such file");
// msgBox.exec();
// return 1;
// }
// if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
// {
//
// QMessageBox msgBox;
// msgBox.setText("Error while opening file");
// msgBox.exec();
// return 1;
// }
// QTextStream in_stream(&file);
//std::string line=in_stream.readLine().toStdString();
//
//while(true)
// {
// process_line(line, my_map);
// line = in_stream.readLine().toStdString();
// if(in_stream.atEnd()) break;
// }
//this was the original QT code i used, but then i had to switch to std containers, so i had to make some twitches.
return 0;
}
To copy to clipboard, switch view to plain text mode
and this is my main.
#include "downloader.h"
#include "parser.h"
#include <QApplication>
int main(int argc, char *argv[])
{
std::map<std::string,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
return a.exec();
};
#include "downloader.h"
#include "parser.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::map<std::string,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
return a.exec();
};
To copy to clipboard, switch view to plain text mode
Can someone tell what im missing here?
edit: full code
Bookmarks