Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new File with full path in Qt?

Tags:

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).

like image 694
GG. Avatar asked Jun 11 '10 16:06

GG.


People also ask

How do you create a file and write it in Qt?

Your current working folder is set by Qt Creator. Go to Projects >> Your selected build >> Press the 'Run' button (next to 'Build) and you will see what it is on this page which of course you can change as well. Show activity on this post. It can happen that the cause is not that you don't find the right directory.

How do you write data to a file in Qt?

To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right. In the following code, we open a new file with the given name and write a text into the file. Then, we read in the file back and print the content to our console.


2 Answers

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.

Qt's caveat for file creation

Platform Specific Issues

File permissions are handled differently on Unix-like systems and Windows. In a non writable directory on Unix-like systems, files cannot be created. This is not always the case on Windows, where, for instance, the 'My Documents' directory usually is not writable, but it is still possible to create files in 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.

like image 152
György Andrasek Avatar answered Sep 23 '22 15:09

György Andrasek


AFAIK it is not possible to create the file and the directory directly with QFile. You have to first create the directory (QDir::mkpath will create the full path) and then the file (QFile::open).

QString path("SomePath/NewDirectoryA/"); QDir dir; // Initialize to the desired dir if 'path' is relative           // By default the program's working directory "." is used.  // We create the directory if needed if (!dir.exists(path))     dir.mkpath(path); // You can check the success if needed  QFile file(path + "NewFile.kml"); file.open(QIODevice::WriteOnly); // Or QIODevice::ReadWrite 
like image 40
ymoreau Avatar answered Sep 25 '22 15:09

ymoreau