Hi, i'm making an currency converter app.
I go online, download a html and save it into a txt file, which i parse afterwards for the exchange rates.

.h
Qt Code:
  1. #ifndef DOWNLOADER_H
  2. #define DOWNLOADER_H
  3.  
  4. #include <QObject>
  5. #include <QNetworkAccessManager>
  6. #include <QNetworkRequest>
  7. #include <QNetworkReply>
  8. #include <QUrl>
  9. #include <QFile>
  10. #include <QDebug>
  11. #include <QMessageBox>
  12. #include <string>
  13. #include <Windows.h>
  14. #include <qstring.h>
  15.  
  16.  
  17. class downloader : public QObject
  18. {
  19. Q_OBJECT
  20. public:
  21. explicit downloader(QObject *parent = 0);
  22.  
  23. void Do_download();
  24.  
  25. public slots:
  26. void replyFinished (QNetworkReply *reply);
  27.  
  28. private:
  29. QNetworkAccessManager *manager;
  30.  
  31. };
To copy to clipboard, switch view to plain text mode 

.cpp

Qt Code:
  1. #include "downloader.h"
  2. #include "strings.h"
  3. #include <string>
  4. #include <Windows.h>
  5.  
  6. downloader::downloader(QObject *parent) :
  7. QObject(parent)
  8. {
  9.  
  10. }
  11.  
  12.  
  13. void downloader::Do_download()
  14. {
  15. manager = new QNetworkAccessManager(this);
  16.  
  17. connect(manager, SIGNAL(finished(QNetworkReply*)),
  18. this,SLOT(replyFinished(QNetworkReply*)));
  19.  
  20. manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
  21. }
  22.  
  23. void downloader::replyFinished(QNetworkReply *reply)
  24. {
  25. if(reply->error())
  26. {
  27. QMessageBox msgBox;
  28. msgBox.setText(strings::msgBoxDownReplyError);
  29. msgBox.exec();
  30. }
  31. else
  32. {
  33.  
  34. QFile *file = new QFile("C:/Qt/test/downloaded.txt");
  35. QByteArray data = reply->readAll();
  36.  
  37. if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
  38. {
  39. file->write(data);
  40. file->flush();
  41. file->close();
  42. }
  43. delete file;
  44.  
  45. }
  46. reply->deleteLater();
  47.  
  48. }
To copy to clipboard, switch view to plain text mode 

parser.h

Qt Code:
  1. #ifndef PARSER_H
  2. #define PARSER_H
  3.  
  4.  
  5. #include <QMessageBox>
  6. #include <QStringList>
  7. #include <string>
  8. #include <sstream>
  9. #include <stdlib.h>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <string>
  14. #include <Windows.h>
  15.  
  16.  
  17. class parser
  18.  
  19. {
  20.  
  21. public:
  22. explicit parser();
  23.  
  24. QStringList currency_list;
  25. std::list<std::string> curr_list;
  26.  
  27. void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
  28. int read_line(std::map<std::string,double> &my_map);
  29.  
  30. void init_list();
  31.  
  32. std::string getExePathParser();
  33.  
  34. private:
  35.  
  36. };
  37.  
  38. #endif // PARSER_H
To copy to clipboard, switch view to plain text mode 


parser.cpp

Qt Code:
  1. #include "parser.h"
  2. #include "calculator.h"
  3. #include "strings.h"
  4. #include <string>
  5.  
  6. parser::parser()
  7. {
  8. init_list();
  9.  
  10. }
  11. void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
  12. {
  13. double curr;
  14. std::string temp;
  15.  
  16. temp = line.substr(52,8);
  17.  
  18. std::replace(temp.begin(),temp.end(),',','.');
  19.  
  20. curr = std::stod(temp,NULL);
  21.  
  22. my_map.insert(std::make_pair(curr_string,curr));
  23.  
  24. }
  25.  
  26.  
  27. int parser::read_line(std::map<std::string,double> &my_map)
  28. {
  29.  
  30. std::list<std::string>::iterator iter;
  31. std::string linija;
  32. std::ifstream file_("C:/Qt/test/downloaded.txt");
  33.  
  34. if(file_.fail())
  35. {
  36. QMessageBox msgBox;
  37. msgBox.setText(strings::msgBoxOpeningError);
  38. msgBox.exec();
  39. }
  40.  
  41. else if(file_.is_open())
  42. {
  43. while(std::getline(file_,linija))
  44. {
  45. for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
  46. {
  47. if(linija.find(*iter) != std::string::npos)
  48. process_line(linija, my_map, *iter);
  49. }
  50. }
  51. file_.close();
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57.  
  58.  
  59. void parser::init_list()
  60. {
  61. currency_list << "HRK" << "CAD" << "CZK" << "DKK" << "HUF"
  62. << "JPY" << "NOK" << "SEK" << "CHF" << "GBP"
  63. << "USD" << "EUR" << "PLN" << "AUD";
  64. curr_list.push_back("HRK");
  65. curr_list.push_back("CAD");
  66. curr_list.push_back("CZK");
  67. curr_list.push_back("DKK");
  68. curr_list.push_back("HUF");
  69. curr_list.push_back("JPY");
  70. curr_list.push_back("NOK");
  71. curr_list.push_back("SEK");
  72. curr_list.push_back("CHF");
  73. curr_list.push_back("GBP");
  74. curr_list.push_back("USD");
  75. curr_list.push_back("EUR");
  76. curr_list.push_back("PLN");
  77. curr_list.push_back("AUD");
  78.  
  79.  
  80. }
To copy to clipboard, switch view to plain text mode 

and my main

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4.  
  5. std::map<std::string,double> currency_map;
  6.  
  7.  
  8. downloader d;
  9. d.Do_download();
  10.  
  11.  
  12. parser p;
  13. p.read_line(currency_map);
  14.  
  15.  
  16. MainWindow w(currency_map);
  17. w.show();
  18.  
  19.  
  20. return a.exec();
  21. };
To copy to clipboard, switch view to plain text mode 

It was working good, but then i changed the path for the .txt file i create.
And the first time i run the app, i get a msgBoxError from my parser class
Qt Code:
  1. if(file_.fail())
  2. {
  3. QMessageBox msgBox;
  4. msgBox.setText(strings::msgBoxOpeningError);
  5. msgBox.exec();
  6. }
To copy to clipboard, switch view to plain text mode 

this part of code pops up.
then i shut it down. run the app again, and it's all working well.
so everytime i change the file path, the first running of the app wont work.
can someone help me out, and spot what's wrong here?

(i have to use basic c++ in some parts of the code, don't ask)