Qfile Question - hard coded path
Currently I'm reading content out of a USB stick that is hard coded in my software.
Code:
QFile file("W:/TemperatureReadings/date.xls");
I want to change this so it isn't a hard coded path anymore and the software will recgonize any drive letter with folder (TemperatureReadings) and file name (date.xls)
Is there an easy way to do this? I can't figure it out from the documentation.
Thanks a lot!
Re: Qfile Question - hard coded path
Get all drives via QDir::drives(), then "search" in all drives for a folder TemperatureReading using QDir::cd() (return value) and then check for the file. You can also use QFile::exists() with a constructed path directly.
Re: Qfile Question - hard coded path
Thanks for the reply Lykurg!
Could I just do? -
Code:
QFile file("/TemperatureReadings/date.xls");
file.open(IO_WriteOnly | IO_Translate | IO_Append);
I've never used QDir before so I'm kind of confused on how I would write out what you suggested.
Thanks again
Re: Qfile Question - hard coded path
Quote:
Originally Posted by
fortyhideout12
Could I just do? -
Code:
QFile file("/TemperatureReadings/date.xls");
file.open(IO_WriteOnly | IO_Translate | IO_Append);
I thought you want search the coputer drives for a that folder and file. Can't see that your code is doing that.
Since you want to learn something a little bit more elaborate:
- Get all drive names (Like c:, d:, f: etc.)
- Construct the path depending on the drives
- Check for file.
QDir::drives returns a list of QFileInfos. See QList on how to iterate over a list. For each drive construct the path for drive (one list entry) and your specified folder and file name. (QDir::separator() is useful). Check if the file exist via QFile::exists(). If it exist, read it.