Get file names from from folder C++ - c++

I am looking for a way to get the file names of files in a specific folder. I have search and many people say they use the boost.filesystem. I do not know how to include this in my c++ file/package to be able to use it, or ever how to use it. Is there a different method that might have been implemented. I am using Visual Studio for building my C++ program.
It would also need to be able to prompt the user folder specific folder/file.

One API available on almost all platforms is opendir()/readdir():
Open directory using C
DOS programs used findfirst()/findnext()/findclose(), followed by FindFirst()/FindNext() in Win32:
http://stanislavs.org/helppc/findfirst.html
http://msdn.microsoft.com/en-us/library/zyzxfzac.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx

Related

How to create dynamic defines for Visual Studio?

I have a C++ project that builds on several platforms.
On Mac OSX and Linux, I use SConstruct, which allows me to have some "smartness" regarding the different compilation steps. Namely, I could put the program version in a file named VERSION at the root of the repository, whose content is simply:
2.0
In the build SConscript, I just have to open, read and parse that file and I can create dynamic defines based on it. For instance:
env.Append(CXXFLAGS=['-DVERSION_MAJOR=%s' % open('VERSION').read().split('.')[0]])
This is, for obvious reasons, very convenient. It also allows me to put today's date in an environment variable for instance.
Now for Windows, I have a .sln file with different .vcxproj files into which I'd like to do something similar, except I have no idea how.
To summarize, my question is: how can I have "smart" defines like that (reading, parsing a file and putting its content into several environment variables) without having to change the .sln/.vcxproj files manually on every version shift ?
I know I could use SCons on Windows too, but I'd like not to (mainly because it seems less popular on the platform, and I don't want to scare potential contributors that only know Windows-specific tools).
A common way to do this is to define your constants in an include file:
e.g.
// Version.h - Autogenerated, don't edit
#define VERSION_MAJOR 1
Next you write a script or a program (in your favourite language) to obtain version from somewhere and dynamically write Version.h. Possibly parse the old Version.h and increment or get it from some external source.
In visual studio, create a custom build step for Version.h and make it dependent on something that forces it to update on every build.
You could maintain the current solution, and for Windows, integrate it with Visual Studio solution and project files generated by SCons using the MSVSProject() builder or the MSVSSolution() builder.
You can find more info about these SCons builders here.

new to eclipse and c++: how does a c++ project get deployed?

i am using c++ in eclipse, a project which contains many header files , cpp files...etc when finally done, how does it actually get implemented in the real world once it is done? Does an .exe file get created ? Or how can users install the program on their computers?
Can you share your experience with me ?
Also can you tell me what IDE you use for C++ development ?
I tried to look through some documentation but could not find anything.
When you compile the code, it will output an executable file (.exe) that can then be run from a command line, double-clicked, or put into an installer. The executable files name will be .exe and placed in the output directory as specified in your project settings.
As mentioned, this can be bundled in an installer but that is very likely overkill and way beyond what you want. So long as you have used standard C/C++ libraries that come installed on a user's system, they can just invoke the program from a command line or simply double-click the executable. If your program doesn't have any interface and simply prints messages, then they person will want to run it from the command line. Finally, if you have used other libraries that you need to include with your program, then an installer may be what you need in order to make sure the end user has everything they need to run your program.
As far as IDE's go, I've used both Eclipse and Visual Studio. Visual Studio is better tailored to C/C++ development (this is assuming you are programming on a Windows machine) but has the downside of costing money if you don't get it through your employer or school. If you can get access to it through either of those channels, I would choose it over Eclipse. Eclipse will do what you want and is free, but Visual Studio might be a bit better. Also, if you are going to spend money anyway, I suggest looking a SlickEdit. I use this at work and really like it for C/C++ development.

Making NetBeans Include Directories the IDE Knows About

I've been developing a lot in Java lately but I've been asked to look at switching one of my projects to C++ and I'm having a bit of trouble setting up the includes. Unfortunately the extent of my C++ knowledge is a couple of academic projects I did in college.
I have created a project in NetBeans and I'm trying to add in some headers from an external library (pugixml). I have
#include "pugixml.hpp"
at the top of the file I intend to use it in. The problem is that when I compile I get
main.cpp:9:23: pugixml.hpp: No such file or directory
which seems odd to me seeing as the IDE definitely knows where the file is. Code completion on classes from inside the library works and holding control and mousing over the file name shows me the correct directory where the file lives. How do I convince the compiler to include this directory?
I know this really feels like something that should be easy to Google, but I haven't had any luck.
if you are using a makefile on linux:
check your makefile and add the -I Path_to_your_pugixml_hearders in the option of its compilation recipe.
or, add your path to the environment variable $CPLUS_INCLUDE_PATH. for example, in bash
export CPLUS_INCLUDE_PATH=Path_to_your_pugixml_hearder:$CPLUS_INCLUDE_PATH

Read multiple files

I'm new to c++ and am trying to alter the console app code posted below to read multiple files, ideally using a wildcard extension. Can some please give me some pointers..?
http://msdn.microsoft.com/en-us/library/ms916815#odc_wssusageeventlogging_examiningtheusagelogfileformat
-----------Edit-------
What I need is how to change the code above instead of pointing it to a specific [filename.log] point it to a directory name and let it process all the log files in that directory.
--------------Tools-----
Win32 Console Application project in Visual Studio 2010 in C++
[To be run on win 32 bit platform]
Using Win32 APIs you can list the files in a directory by following this example. From there it should be relatively trivial for you to incorporate that code into your application to allow you to process multiple files as requested.
Specifically the FindFirstFile API allows for wildcard when search for files.
If you're willing to use the boost library check out
this post. If you're using something like C++/CLI then there is support in .NET for this as well (I'm assuming for now you're not using C++/CLI). If you specify the tools at your disposal maybe you can get a more directed answer.

Determining files in a directory

I come from a C# background and I am working on a C++ project. I need to open files in a directory, then process that data in the files. The problem is on my target environment (Greenhills Integrity), I cannot access a "directory". It seems C++ does not have a concept of a directory. Why not? This problem is simple in C#. I cannot link to any big library(BOOST or dirent) to get the files. I can open a file using fopen, but I won't always know the file names, so I have to "strcat" the directory to each filename in order to "fopen" the files.
I need a way to just get the file names in a directory without using an external API. Is that possible?
The major C++ APIs have directories. Start with readdir on POSIX or FindFirstFile() on Windows. Greenhills seems to support POSIX.
No, it's not possible. C++ has no "built-in" directory functionality - you need to use a library of some sort.
Check with your operating system. Directory handling is different for each. You will have to use the Windows 32 API if you want to list/query directories on Microsoft Windows, and the Linux API (e.g. opendir/stat) if you want to list/query directories on Linux.