tags:

views:

167

answers:

2

Hello friends,

I am a Qt beginner and just got stuck with the problem. I am looking for a file SomePath/NewDirectoryA/NewFile.kml ( NewFile.kml will be the only file in NewDirectoryA, having this directory just to maintain semantics in the project ).

If SomePath/NewDirectoryA/NewFile.kml exists then I will use it in my code and If it doesn't exist then I have to create it. If this File doesn't exist then this directory also doesn't exist in SomePath. So If only I have to create a file I can use QFile and open it in ReadWrite or WriteOnly mode.

But the problem is I have to create the file with the directory itself. I tried with QFile with file name SomePath/NewDirectoryA/NewFile.kml but it didn't worked. Please suggest me a way in which I can create a new file( NewFile.kml ) in a new directory( NewDirectorA ) at a given location( SomePath ).

A: 

The link should help you.

yesraaj
+3  A: 

bool QFile::open ( OpenMode mode ) [virtual]

[...]

Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

Directories are created with

bool QDir::mkdir ( const QString & dirName ) const

Creates a sub-directory called dirName.

and

bool QDir::mkpath ( const QString & dirPath ) const

Creates the directory path dirPath.

The function will create all parent directories necessary to create the directory.

Jurily
...or QDir::mkpath() to create multiple directories
Frank
Thanks. Edited in.
Jurily
can't I create a file with all necessary parent directories ?
GG
btw thanks, it helped me.
GG