C++Introduction to C++Language FeatureLearn C++

How To Use GetTempPath Method In C++ Builder?

How can I get a GetTempPath from the operating system? How we can use GetTempPath Method in C++? How can I easily find the location of the Temp Directory? Let’s answer these questions.

C++ Builder has a lot of specific methods in its SysUtils library that is included in vcl and fmx libraries. Some of these are grouped as Path Manipulation Routines which allow the user to edit, extract, get and set drive name, directory name, file name, file extensions among other things. These methods are combined in Vcl.FileCtrl, System.IOUtilsSystem.SysUtils libraries. These methods are easy to use and make it simple to get or set file path strings in the operating system. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files or directories to see if they are exist or not in that given path. We can also get a temp directory path from the operating system to use in our programs as a safe place to store transient or volatile working information which we want to remove when we are finished with it.

Let’s see how we can use GetTempPath Method in C++ Builder.

What is the GetTempPath method?

The GetTempPath method (System:IOUtils:TPath:GetTempPath) is a Path Manipulation Routine that returns the path to a directory to store temporary files. This directory is a system-managed location; files saved here may be deleted between application sessions or system restarts. You cannot – and should not – count on it being safe to store anything between launches of your program.

If the system running your application does not support the requested folder, or if the requested folder does not exist in the system, this function returns an empty string instead. GetTempPath points to the following locations on the various platforms:

  • On WindowsOS X, and Linux it points to a system-wide directory.
  • On iOS and Android, it points to a user-specific, application-specific directory.
PlatformSample path
Windows XPC:\Documents and Settings\<User name>\Local Settings\Temp
Windows Vista or laterC:\Users\<User name>\AppData\Local\Temp
OS X/var/folders/<random folder name>
iOS Device/private/var/mobile/Applications/<application ID>/tmp
iOS Simulator/Users/<username>/Library/Application Support/iPhone Simulator/<SDK version>/Applications/<application ID>/tmp
Android/storage/emulated/0/Android/data/<application ID>/files/tmp

What is the Syntax of the GetTempPath method in Modern C++?

Here is the Syntax of GetTempPath() Method,

Is there a simple example of using the GetTempPath Method in Modern C++?

GetTempPath() Method returns a UnicodeString, We can get Temp Path of the system from a given path string as below,

What about a full Example of how to use the GetTempPath Method in Modern C++?

Here is the full C++ Builder VCL example with the Memo (TMemo) component below,

In addition, we can get Home Folder Path by using GetHomePath Method. We can also get all Environment Variables by using the GetEnvironmentVariable Method. These methods will be explained in another posts.

Where can I learn more about path manipulation routines in C and C++?

If you are looking to Paths and Directories of C++ Builder IDE Options, they are listed here. C++ Builder Compiler Directories and Conditionals are listed here Environment variables are listed here.

Click the following dynamic search link to see other examples of path manipulation routines on this blog: https://learncplusplus.org/?s=path

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsLanguage FeatureLearn C++

How To Add Shadow Effects To Your C++ Apps

C++ComponentsLanguage FeatureLearn C++

How To Make Controls Have A Glow Effect In C++?

C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

C++C++17Introduction to C++Language FeatureLearn C++

This Is How To Use Parallel Programming in C++ Builder