c++, List all files, dirent.h on Windows - c++

in C++, what would be the best way to list all files of a directory on Windows?
On Linux or on Windows using gcc (e.g. MingW) this is easy possible with dirent.h, but what's the best way to do it on Windows when dirent.h is not available (e.g. Visual Studio)?
Should I force people do add a freely available implementation of dirent.h to their Visual Studio? Or should I write an alternative code for reading the files? If this, what would be the best code to do so?
Thanks.

dirent.h is a POSIX header. On Windows you use the Find*File*() functions in the Win32 API.

Something like Boost Filesystem would work across all of those situations.

To add to the answer above, Windows API is accessed via the windows.h header file included.

Related

Unable to open the socket program header in VS2008

idevs.h, netinet/in_systm.h, netinet/ip.h, netinet/tcp.h openssl/ssl.h sys/socket.h
These header files can work in Linux but in visual studio 2008 compile error says unable to open header file. These are socket program related headers. (I am unable to get any proper result from web search)
Problem:
Please let me know any dll I have include for these headers or any other equivalent headers are available ?
Thanks in advance.
In windows environment you need to include the windows specific headers like winsock.h and others (http://msdn.microsoft.com/en-us/library/windows/desktop/ms738545(v=vs.85).aspx). You need to switch between headers using the #ifdef statements when doing builds for different platforms.
Nobody ever promised that windows implementation of the sockets concept is 100% identical to the one of Unix. These implementations have a lot in common, but differences are also present.
Sockets are not part of the C++ standard and are implemented in different ways in Linux and Windows. That means, that the native socket libraries are different in both OSes, and Windows has other headers for its socket API than Linux. So you will not only have to include other headers but might also need to use other functions.
Depending on what you want to achieve, you might want to use a library that wraps the OS specific parts and provides a portable interface. There are several more or less portable networking libraries, one of the best known might be Boost.Asio

direct.h documentation

I'm trying to write a small block of code that prints out the files in a given directory. I've seen references to using the direct.h library to do this, but I cannot find any documentation whatsoever on the header file and its methods. Is this library outdated? And is there another way to list all filenames of a directory in C++?
It seems that this header functions are not designed for this task, See http://msdn.microsoft.com/en-us/library/as5kw0ze(v=vs.110).aspx
Use FindFirstFile, FindNextFile and FindClose loop like in this example code
dirent.h is header which works cross platform. So I would recommend to use it instead of some Windows functions. On some compilers it's not included as a standart header, for example Visual Studio but you can use it.

Using pthread.h on a windows build

I have a codebase that makes extensive use of pthread.h. On a windows visual studio project, this obviously doesn't work since pthreads is a unix utility.
I know there exists pthread-win32 or something of the sort, but is there a way to have a codebase use this, without replacing all of the pthread code?
edit: I'd prefer to not have to go through and replace all the pthread calls. This is my question
Try http://sourceware.org/pthreads-win32/. While I've never used that particular library, I've had luck using some windows ports/abstraction layers of POSIX APIs.
I would recommend boost as a library that contains a platform-independent thread abstraction. I don't think you'll find any attempt to call the pthread functions themselves to be satisfying. It's also not very hard to map pthread to Win32 for yourself.
If the code is very heavily *nix-based, you'll probably have the best luck compiling it with Cygwin+GCC. If pthreads is the only *nix dependency and you'd like to avoid the Cygwin dependency, then you should go with Pthreads-win32.

How can I configure my project to generate platform independent code?

I am writing an application that I would like to release binaries for on Mac, Windows, and Linux. I have code that compiles under Mac and Linux, but under Windows, it does not.
This is because of Windows lack of a strcasecmp. I've read a little bit about how I can create some sort of header to wrap my code, but I don't really understand this concept too well. I've worked on the code on my Mac with just vim and make, but now I'm trying to switch it all over to Visual Studio.
Is there some way I can set my project up to include Windows wrapper headers when I'm building on Windows, but omit them when I'm building on my Mac or Linux box?
This problem is really giving me a headache and I'd appreciate any suggestions!
You could do
#ifdef WIN32
#include <windows_specific_header.h>
#else
#include <other_header.h>
There is also an MS Visual Studio-specific macro: _MSC_VER, so
#ifdef _MSC_VER
would also work here.
There is also WINVER define in windows.h.
configure my project to generate platform independent code
That is a bit of an odd phase, so I'm not sure that I'm aswering the right question, but here goes:
You have to write platform independent code.
Do one of these:
Write to a cross-platform framework (i.e. QT)
Only use library functions that are available on all your targets
or
provide wrappers to fill up any gaps in the library for on (or more) targets
Boost libraries are designed to be cross-platform. In particular, if you need to manipulate strings, you'll probably find what you need. And it will be cross-platform without having to deal with it yourself. See there to get a glimpse of what's available.
maybe you can consider compiling your code with MINGW32 on windows.

Reading file names with C++

Is there a way to read file names from a folder using purely C (or C++)? That means without including windows.h (no FindFirstFile(), etc...).
It doesn't look like fstream has this functionality. I know that file names are operating system dependent, but I was hoping there is some library that will allow it in Windows.
boost filesystem is a nice solution. Of course under the hood, it will still be using the windows API calls (when you build on windows), but this is abstracted away from you.
C++ typically does not supply you with such functionality. A cross-platform solution is to use boost::filesystem.
Try the POSIX functions opendir() and readdir() for iterating through directories. See this link for the manual page with some great example code. These functions should be available on most platforms, both Windows and UNIX.
If you wish to use opendir() and readdir() on windows, you can download MinGW, a windows port of the famous GNU compiler collection. It includes windows ports of the UNIX header files, including dirent.h, which will allow you to use the specified functions. Keep in mind these will call native API's either way.
-John