PDA

View Full Version : Some confusion with creating and downloading a txt file



bikonja
25th August 2015, 11:35
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>



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


downloader.cpp


#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();

}


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();

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

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())
{
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;
}

and this is my main.


#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();
};


Can someone tell what im missing here?

edit: full code

ChrisW67
25th August 2015, 11:57
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.

bikonja
25th August 2015, 12:48
I edited the code, should compile now i belive

Frdz
25th August 2015, 14:55
QFile *file = new QFile(strings::filePathQt); //your downloaded file here

std::ifstream file_("C:/Qt/test/downloaded.txt"); //then read the file in this line to get parsed later
I think those 2 line has corresponding relation
Get more information about "filePathQt" ^^
Parabens

bikonja
25th August 2015, 15:29
I'm not sure what you mean.



#pragma once
#ifndef STRINGS_H
#define STRINGS_H

#include <qstring.h>


class strings

{
public:
strings();

static const std::string filePathStd;

static const QString filePathQt;
static const QString msgBoxDownReplyError;
static const QString msgBoxOpeningError;
static const QString msgBoxReplyError;

};

#endif // STRINGS_H


#include "strings.h"

strings::strings()
{

}
const std::string strings::filePathStd = "C:/downloaded.txt";

const QString strings::msgBoxDownReplyError = "Error while reading";
const QString strings::filePathQt = "C:/downloaded.txt";
const QString strings::msgBoxOpeningError = "File opening failed";
const QString strings::msgBoxReplyError = "Error while reading";


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.

Frdz
25th August 2015, 16:12
I didn't see "filePathStd" used in your previous code. :confused:

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
^^

bikonja
25th August 2015, 16:19
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.

Frdz
25th August 2015, 17:13
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
^^

bikonja
25th August 2015, 18:25
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? :p

ChrisW67
25th August 2015, 21:42
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