How to search a file in subdirectories? - c++

I have to create a program that search the file "letter.txt" in the subdirectories of a specific directory ( folderA in the example below ).
For example:
folderA/
folderA1/...
folderA2/...
folderA3/...
folderA4/letter.txt
folderA5/...
What API do I have to use to:
- list subdirectories of a specific directory ( folderA ),
- open each of these subdirectories ( folderA1, folderA2, folderA3, etc )
- search and open the file letter.txt ?

In raw Win32, this would be done using FindFirstFile, but you will have to code the recursion manually; it's going to be hard to get all the corner cases right (e.g. what happens if you have reparse points that create a cyclic directory structure?).
For more convenience you can use Boost.Filesystem; class recursive_directory_iterator does exactly what you want. In the past I have also used the recls library and I was quite satisfied -- it just works.

The best way I know how to do that task is to use the Boost.filesystem library.
http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/index.htm
It provides a directory iterator (and a recursive directory iterator) that allows you to iterate through files and directories in your directory looking for the file you need need.

Related

"Embedding" a folder into a C/C++ program

I have a script library stored in .../lib/ that I want to embed into my program. So far, that sounds simple: On Windows, I'd use Windows Resource Files - on MacOS, I'd put them into a Resource folder and use the proper API to access the current bundle and it's resources. On plain Linux, I am not too sure how to do it... But, I want to be cross-platform anyway.
Now, I know that there are tools like IncBin (https://github.com/graphitemaster/incbin) and alike, but they are best used for single files. What I have, however, might even require some kind of file system abstraction.
So here is the few guesses and estimates I did. I'd like to know if there is possibly a better solution - or others, in general.
Create a Zip file and use MiniZ in order to read it's contents off a char array. Basically, running the zip file through IncBin and passing it as a buffer to MiniZ to let me work on that.
Use an abstracted FS layer like PhysicsFS or TTVFS and add the possibility to work off a Zip file or any other kind of archive.
Are there other solutions? Thanks!
I had this same issue, and I solved it by locating the library relative to argv[0]. But that only works if you invoke the program by its absolute path -- i.e., not via $PATH in the shell. So I invoke my program by a one-line script in ~/bin, or any other directory that's in your search path:
exec /wherever/bin/program "$#"
When the program is run, argv[0] is set to "/wherever/bin/program", and it knows to look in "/wherever/lib" for the related scripts.
Of course if you're installing directly into standard locations, you can rely on the standard directory structure, such as /usr/local/bin/program for the executable and /etc/program for related scripts & config files. The technique above is just when you want to be able to install a whole bundle in an arbitrary place.
EDIT: If you don't want the one-line shell script, you can also say:
alias program=/wherever/bin/program

Set Output directory in C++ one folder up

I am using a header library in my code to set my output directory. I would be running almost 12000 executables in parallel and I want a common output folder which is one level up. Here is how I use it
global::directories().setOutputDir("./outputfolder/");
where outputfolder is a folder in the current directory with the executable. I would like this string to be one folder up directory (somethong like ../outputfolder)
Unfortunately setOutputDir takes inputs as string (http://www.palabos.org/documentation/develguide/globalDefs_8h_source.html)
I looked around and found that boost library would be the most appropriate way to achieve what I want. Just wanted to know if there can be a workaround using the standard C++ before I delve into the library
Thanks..

Waf: Recursively collect source files and include paths

My C-gcc project structure is:
\Project\wscript (only one in project)
\Project\build\
\Project\Source\Module_1\foo.c
\Project\Source\Module_1\foo.h
\Project\Source\Module_1\dummy\foo2.h
\Project\Source\Module_n\bar.c
\Project\Source\Module_n\any dept...\bar.h
How can I recursively find all *.C files in 'Source' and add to
bld.program(source=HERE)?
instead of manually listing it:
bld.program(source="foo.c bar.c...", includes='..\\Source ..\\Source\Module_1')
Also how can I find every subfolders (preferably which has *.h) and append to include path?
Should I write my own finder functions in python and just pass it?
bld.program(source=SRCs_FOUND, includes=Paths_FOUND)
Will this cause any dependency problems in building?
In any modern IDE this thinking is common, drag one file to the Source tree, and it's automatically added to the build list. Thanks!
You can use globbing to scan the directories.
bld.program(
name = ...
....
source = bld.path.ant_glob('**/Source/*.C')
)
Just search for ant_glob in the waf book.

Get list of files in application directory

I am developing a C++ application for win32 console
I need to get list of files in my application directory
(for example if my application had been started in C:\arash\app\ I need list of files in this folder)
I searched and find FindFirstFile function in windows.h header , But this function need a directory path .
Can I use this function for getting list of files in my application running directory?
Thanks
Use GetModuleFileName() with a NULL module handle to get the path and filename of the .exe file. You can then strip off the filename portion, and use the remaining path as needed.
The current working directory is '.'.
As noted in comments, this isn't necessarily the directory you want.

Header file inside the same solution

http://youtu.be/6NCtnKcwOas (select better quality!)
As you can see on the attached video, I have the two projects in my solution - a dll creator and a simple testing project. Just followed this tutorial .
Why does the MathFuncsDll.h still remain undetected?Everything works fine after specifying the full path after '#include'. However, I don't want to use such rough-and-ready method because it looks messy and unprofessionally.
If you can specify the file using an absolute path, but not by only using its filename, the compiler doesn't "know" about the folder containing that file.
You can tell the compiler about your additional include directories via the /I directive (documentation). And of course you can set that via the IDE.