Results 1 to 10 of 10

Thread: Some confusion with creating and downloading a txt file

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Some confusion with creating and downloading a txt file

    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

    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.  
    13.  
    14.  
    15. class downloader : public QObject
    16. {
    17. Q_OBJECT
    18. public:
    19. explicit downloader(QObject *parent = 0);
    20.  
    21. void Do_download();
    22.  
    23. public slots:
    24. void replyFinished (QNetworkReply *reply);
    25.  
    26. private:
    27. QNetworkAccessManager *manager;
    28.  
    29.  
    30. };
    31.  
    32. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    downloader.cpp

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

    then im using this code to parse the file

    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.  
    14.  
    15. class parser
    16.  
    17. {
    18.  
    19. public:
    20. explicit parser();
    21.  
    22. QStringList currency_list;
    23. std::list<std::string> curr_list;
    24.  
    25. void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
    26. int read_line(std::map<std::string,double> &my_map);
    27.  
    28. void init_list();
    29.  
    30. private:
    31.  
    32. };
    33.  
    34. #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.  
    5. parser::parser()
    6. {
    7. init_list();
    8.  
    9. }
    10. void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
    11. {
    12.  
    13.  
    14.  
    15. double curr;
    16. std::string temp;
    17.  
    18. temp = line.substr(53,8);
    19.  
    20. std::replace(temp.begin(),temp.end(),',','.');
    21.  
    22. curr = std::stod(temp,NULL);
    23.  
    24. my_map.insert(std::make_pair(curr_string,curr));
    25.  
    26. }
    27.  
    28. int parser::read_line(std::map<std::string,double> &my_map)
    29. {
    30.  
    31. std::list<std::string>::iterator iter;
    32. std::string linija;
    33. std::ifstream file_("C:/Qt/test/downloaded.txt");
    34.  
    35. if(file_.fail())
    36. {
    37. QMessageBox msgBox;
    38. msgBox.setText("File opening failed");
    39. msgBox.exec();
    40. }
    41.  
    42. else if(file_.is_open())
    43. {
    44. while(std::getline(file_,linija))
    45. {
    46. for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
    47. {
    48. if(linija.find(*iter) != std::string::npos)
    49. process_line(linija, my_map, *iter);
    50. }
    51. }
    52. file_.close();
    53. }
    54. // if(!file.exists())
    55. // {
    56. //
    57. // QMessageBox msgBox;
    58. // msgBox.setText("There is no such file");
    59. // msgBox.exec();
    60. // return 1;
    61. // }
    62.  
    63. // if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    64. // {
    65. //
    66. // QMessageBox msgBox;
    67. // msgBox.setText("Error while opening file");
    68. // msgBox.exec();
    69. // return 1;
    70. // }
    71.  
    72. // QTextStream in_stream(&file);
    73. //std::string line=in_stream.readLine().toStdString();
    74.  
    75.  
    76. //
    77. //while(true)
    78. // {
    79. // process_line(line, my_map);
    80. // line = in_stream.readLine().toStdString();
    81. // if(in_stream.atEnd()) break;
    82. // }
    83.  
    84. //this was the original QT code i used, but then i had to switch to std containers, so i had to make some twitches.
    85.  
    86. return 0;
    87. }
    To copy to clipboard, switch view to plain text mode 

    and this is my main.

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

    Can someone tell what im missing here?

    edit: full code
    Last edited by bikonja; 25th August 2015 at 12:47. Reason: Posted full code

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Some confusion with creating and downloading a txt file

    It helps if you post the actual code; what we have here will not compile.

    The use of forward slashes in Windows file paths is supported in the Qt classes as a convenience. Qt converts the slashes as required. The C++ stream classes likely expect the paths to have backslash path separators. Your attempt to open the file with forward slashes is most likely failing: the file does not exist.

  3. #3
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Some confusion with creating and downloading a txt file

    I edited the code, should compile now i belive

  4. #4
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Some confusion with creating and downloading a txt file

    Qt Code:
    1. QFile *file = new QFile(strings::filePathQt); //your downloaded file here
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. std::ifstream file_("C:/Qt/test/downloaded.txt"); //then read the file in this line to get parsed later
    To copy to clipboard, switch view to plain text mode 
    I think those 2 line has corresponding relation
    Get more information about "filePathQt" ^^
    Parabens

  5. #5
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Some confusion with creating and downloading a txt file

    I'm not sure what you mean.

    Qt Code:
    1. #pragma once
    2. #ifndef STRINGS_H
    3. #define STRINGS_H
    4.  
    5. #include <qstring.h>
    6.  
    7.  
    8. class strings
    9.  
    10. {
    11. public:
    12. strings();
    13.  
    14. static const std::string filePathStd;
    15.  
    16. static const QString filePathQt;
    17. static const QString msgBoxDownReplyError;
    18. static const QString msgBoxOpeningError;
    19. static const QString msgBoxReplyError;
    20.  
    21. };
    22.  
    23. #endif // STRINGS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "strings.h"
    2.  
    3. strings::strings()
    4. {
    5.  
    6. }
    7. const std::string strings::filePathStd = "C:/downloaded.txt";
    8.  
    9. const QString strings::msgBoxDownReplyError = "Error while reading";
    10. const QString strings::filePathQt = "C:/downloaded.txt";
    11. const QString strings::msgBoxOpeningError = "File opening failed";
    12. const QString strings::msgBoxReplyError = "Error while reading";
    To copy to clipboard, switch view to plain text mode 

    i wanted to keep this out of the way to make it more simpler, but i guess i left a hardcoded string in.

    Since i have the file under C:/Qt/test/downloaded.txt the app works, but if i try to change it, the file wont create.

  6. #6
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Some confusion with creating and downloading a txt file

    I didn't see "filePathStd" used in your previous code.

    Since you create file in C:\ drive, it might be any Windows administrators privillege issue
    Just try make a simple program to create file/dir in C:\ to double check
    Or use another drive/folder with user privillege
    ^^

  7. #7
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Some confusion with creating and downloading a txt file

    It's because i replaced strings:: with hardcoded strings to avoid posting to many classes, missed one

    I did you as you said, and that's not the problem.

  8. #8
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Some confusion with creating and downloading a txt file

    Ok Bikonja, then I wanna know which spesific path you meant when your program run correctly?

    Try this one too :
    clear download.txt and redownload from the site, don't run the parse. Check is download txt contain any character or not after that
    ^^

  9. #9
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Some confusion with creating and downloading a txt file

    Thing is the text im downloading is updated daily, so my txt file was also updated daily and i was under the impresion the code was working.

    You actually are right, my code aint creating the file at all. i was using the .txt from my code before i had to make changes.

    Care you help me find out why?

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Some confusion with creating and downloading a txt file

    Now that you have made it clear that creating the file is the problem it would definitely help to know exactly where you are trying to create the file. Windows versions from Vista onward block unprivileged users from writing in certain locations: the Windows system directory, the Program Files directory (-ies) , and the boot drive root directory being the most often abused locations.

    If you want somewhere to store a file then Qt provides a way to look up a suitable location: QStandardPaths
    And a way to create an automatically cleaned up temporary file: QTemporaryFile

Similar Threads

  1. Downloading XML file
    By Abdeljalil in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2014, 22:22
  2. Downloading file problem
    By Kaluss in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2014, 09:50
  3. Proper file downloading?
    By matt4682 in forum Newbie
    Replies: 4
    Last Post: 2nd May 2014, 10:45
  4. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 17:30
  5. File size of a remote file without downloading it
    By dirkdepauw in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2010, 09:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.