Solved Problem with ifstream and Qt Creator
-
@ambershark it doesn ' open the file ... so it should be sth wrong with the path... I printed the Filename which i get with Qstringlist and it looks like that :
("E:/test/file.bin")
-
@BadHombre You should try to remove ios::ate as it is for output but you're opening for reading (ios::in).
Also I'm not sure ifstream supports / in paths on Windows (Qt does). Try to replace / with \\.
From here: http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
"filename
A string representing the name of the file to open.
Specifics about its format and validity depend on the library implementation and running environment." -
Could you show us your .pro file(minimum one please)?Which version of Qt, QtCreator your are using?Are you sure you are using the same compiler to compile your codes?
Besides, you do not need to use raw pointer, there are std::string or std::vector could help you release the memory, in your case std::vector maybe a better choice, because it looks more like a buffer compare with std::string.
if(!file.is_open()){ std::cerr<<"cannot open file\n"; return -1; } auto const size = file.seekg(0, std::ios::end); std::vector<char> buffer(size ); file.seekg(0, std::ios::beg); file.read(&buffer[0], size); //same way to feed the data into std::string file.close();
You could use std::unique_ptr too
auto buffer = std::make_unique<char[]> p(file.tellg()); file.read(buffer.get(), size); //same way to feed the data into std::string
However, I would prefer QFile to ease my pain rather than using std::ifstream
Also I'm not sure ifstream supports / in paths on Windows (Qt does). Try to replace / with \
/ works on my laptop(windows 10 64bits), using Qt5.7, visual c++ 2015 with update 3.
-
Thanks guys for the Help ! ... so the Problem is that getOpenFileNames gives me the Path like that :
("E:/test/file.bin")
to use it in if stream i need zu 2 Slashes after the Drive.. sth like :
("E://test/file.bin")
I tried to use :
QFile f("myfile"); Q if (f.open(QIODevice::ReadOnly)) { }
but it doesn't open the File ... Does sb know how i get a second slash after the without Hardcoding :/ Or why doesn 't qread work ?
-
@BadHombre Why do you want to use ifstream? If you're already using Qt why not use QFile,...?
-
@BadHombre said in Problem with ifstream and Qt Creator:
QFile f("myfile");
Did you check why it doesn't open the file? What is myfile? Absolute path? Relative path?
Did you check http://doc.qt.io/qt-5/qiodevice.html#errorString ? -
@jsulm I tried now to use QFile ... and it seems to be working... but do you kno how i get the doubles of the file... For example read the doubles into a vector.. is this possible ?
-
-
@BadHombre Take a look at http://doc.qt.io/qt-5/qdatastream.html
-
Okay Thanks for your Help Guys :) I also found a way to realise it with ifstream for the people who maybe need it one day...
QStringList filenames = QFileDialog::getOpenFileNames( this, tr("Choose "), "C://", "All files (*.*)"); std::string current_locale_text = filenames[0].toLocal8Bit().constData(); ifstream file(current_locale_text.c_str());
-
@jsulm said in Problem with ifstream and Qt Creator:
Also you do not seek to the beginning of the file before reading from it!
Good catch jsulm! I didn't even see that.