How to write a utility program that lists all contents of directory irrespective of Operating System.
Most of the C/C++ compilers, define macros which can be used to detect operating system. For example, in GCC, following are common macros.
_WIN32 : Defined for both 32 bit and 64 bit windows OS. _WIN64 : Defined for 64 bit windows OS. unix, __unix, __unix__ : Defined in UNIX OS __APPLE__, __MACH__ : Defined in Mac OS Source : StackOverflow
In Windows, we us dir call to list all directories and in most of the other Operating Systems “ls” is used. Below is simple C++ implementation to list directories of folder irrespective of OS.
The above OS independent code is totally different from Java’s platform independence. In Java, there is intermediate byte code that is very clean way of handling platform dependencies. Here we have to remember compiler specific macros and write code in using clumsy #ifdef and #else, and the most important, we need to recompile the code for every OS.
This article is contrubuted by Shubham Agrawal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Writing C/C++ code efficiently in Competitive programming
- Writing code faster during Competitive Programming in C++
- Writing first C++ program : Hello World example
- Facts and Question related to Style of writing programs in C/C++
- Self Destructing Code in C
- Code Bloating in C++ with Examples
- Basic Code Optimizations in C
- Speed up Code executions with help of Pragma in C/C++
- Convert C/C++ code to assembly language
- How to write a running C code without main()?
- Printing source code of a C program itself
- Code to generate the map of India (with explanation)
- Convert C/C++ program to Preprocessor code
- Code valid in both C and C++ but produce different output
- C++: Methods of code shortening in competitive programming
- LEX code to extract HTML tags from a file
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Code Optimization Technique (logical AND and logical OR)