PDA

View Full Version : Qfile Question - hard coded path



fortyhideout12
23rd September 2010, 20:55
Currently I'm reading content out of a USB stick that is hard coded in my software.



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!

Lykurg
23rd September 2010, 21:08
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.

fortyhideout12
27th September 2010, 20:56
Thanks for the reply Lykurg!

Could I just do? -


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

Lykurg
27th September 2010, 21:50
Could I just do? -


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.