C++ search directories and files - c++

Can someone tell me if there is a library built in C++ that I can use to get a list of files and directories? I've looked around and I see people using dirent.h but I need to download it(i think).
Thanks
P.S. I've looked at the fstream, but thats only for reading and outputting files as far as i know.
FORGOT TO MENTION. I DO NOT WANT TO DOWNLOAD ANYTHING, I JUST WANT TO SEE IF THERE IS A LIBRARY THAT IS BUILT WITHIN C++ THAT I CAN USE STRAIGHT OFF THE BAT. THIS IS FOR WINDOWS ASWELL

How about Boost::Filesystem? Supports directory iteration and is portable.

You can use Boost Filesystem library.
http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm
Some nice samples are also provided at the link.
EDIT:
Without downloading a 3rd party library, there is no portable way to do it. For windows you can use CFileFind class from MFC.

Using namespace std::filesystem is available in visual studio 2017 you have in #include
#include <iostream>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <chrono>
#include <thread>
#include <functional>
namespace fs = std::filesystem;
void Files_in_Directory();
fs::path path_Copy_Directory = "E:\Folder";
fs::path path_Paste_Directory = "E:\Folder1";
int main()
{
Files_in_Directory()
return 0;
}
void Files_in_Directory()
{
for (const auto & entry : fs::directory_iterator(path_Copy_Directory))
{
std::cout << entry.path() << std::endl;
}
}

Since others already mentioned boost::filesystem there are also other alternatives. Almost every C++ framework has some way to list directories and files. Like wxWidgets or poco and there are many more.
Regarding dirent.h. It is a standard C Posix library so on Posix compatible systems it should be available. For Windows you can also get it here and it includes instructions on how to use it.
After your edit:
On Windows you can use things like FindFirstFile (example here) and then you don't have to download anything. But it only works on Windows. It is not build in C++.

Related

how to include aspose.pdf.cpp in vs c++ project?

I am trying to implement this basic example https://docs.aspose.com/pdf/cpp/hello-world-example/ but I am getting errors that MakeObject, Document and TextFragment are not defined.
I was developing on vscode but then I realized that aspose can be downloaded through nuget in vs so I switched to vs thinking that adding aspose will be automatically integrated in my project dependencies or something, but no it didn't and I don't know currently how to include it (my use case is also very basic I tried hummuspdf/PDF-Writer but also I didn't knew how to implement it or include it, maybe it isn't as simple as #include "file.h" ? )
#include "Aspose.PDF.Cpp/Document.h"
#include "Aspose.PDF.Cpp/SaveFormat.h"
using namespace Aspose::Pdf;
using namespace System;
int main()
{
auto doc = MakeObject<Document>(u"Example.pdf");
doc->Save(u"Example.docx", SaveFormat::DocX);
}

C++ an VS error: <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED

I coded in C++ on Visual Studio (Windows 10) and got this error:
#error The <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft \
and will be REMOVED. It is superseded by the C++17 <filesystem> header providing std::filesystem. \
You can define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to acknowledge that you have received this warning.
With this headers:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <filesystem>//If I will disable it nothing happens.
#include <experimental/filesystem> //If I will disable it happens another error.
namespace fs = std::experimental::filesystem;
using namespace std;
I've tried: #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING in the main cpp file. It didnt help.
So then I paste this code from here:
#ifdef __cpp_lib_filesystem
#include <filesystem>
using fs = std::filesystem;
#elif __cpp_lib_experimental_filesystem
#include <experimental/filesystem>
using fs = std::experimental::filesystem;
#else
#error "no filesystem support ='("
#endif
Didn't helped too.
What is the easiest way to get out that error?
Add _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to Preprocessor definitions.
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.
This solved the issue for me.
According to the link
C++17’s is supported. This is a completely new
implementation, incompatible with the previous std::experimental
version, necessitated by symlink support, bug fixes, and changes in
standard-required behavior. Currently, including provides
the new std::filesystem and the previous
std::experimental::filesystem, and including
provides only the old experimental implementation. The experimental
implementation will be REMOVED in the next ABI-breaking release of the
libraries.
MS abandoned 'experimental' in filesystem.
You could try to use #include <filesystem>instead of #include <experimental/filesystem> and the std::experimental::filesystem is slated for removal, this means you should use std::filesystem.
I had the same problem and then, when I removed the experimental filesystem and added:
#include <filesystem>
namespace fs = std::filesystem;
I would get the error:
namespace “std” has no member “filesystem”
The solution was to go to project properties and do as instructed in the link.
VS2017: E0135 namespace "std" has no member "filesystem"
Adding the define where necessary worked for me:
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1;
ie.
#ifdef __cpp_lib_filesystem
#include <filesystem>
namespace fs = std::filesystem;
#elif __cpp_lib_experimental_filesystem
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1;
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "no filesystem support ='("
#endif
I've been researching this same problem for weeks, and I'm using visual studio 2019 on an Unreal Engine 4 project. It's difficult to enable c++17 headers in visual studio and make them work. A lot of people online suggests using the boost library instead, but this is my attempt in using filesystem because I don't want to deal with trying to download / import external libraries, and so here's what I know...
** Before you start coding in visual studio, you should build / compile your project and see if Visual Studios needs anything set up; for me, I had to download / setup a free license with Incredibuild (there are tutorials online if someone needs to get this setup). Incredibuild is helpful and necessary to build projects in Visual Studio (at least if you're using Unreal Engine 4), but sometimes I get 1 or 2 "errors" in visual studio, but will compile in Unreal Engine 4 because those errors aren't actually errors (people say online that Incredibuild is weird, and wanted to mention that)**
1) How to enable c++17 Headers
When trying to #include which is a c++ version 17 header, it's necessary to enable c++17: Look inside the visual studio "solution explorer" to view the project files, and right-click the project itself and open "properties". Go to the "C/C++ language" tab and switch the version to c++17 (this is easy to find this online). Unless you're doing an Unreal Engine project because there is not a "C/C++ language" tab, so instead find the "NMake" tab and in the text-box to the right of "Addition Options" type in:
/std:c++17 OR /std:c++latest (Microsoft has a list I found online of others, but these are fine)
and visual studios will recognize as a header.
2) How NOT to get c++17 namespaces to work (things I've tried)
After the headers start working you can try to finally make a namespace variable:
namespace fs = std::filesystem; => ERROR: 'std does not have a namespace or class filesystem'
OR if you also #include you can try:
namespace fs = std::experimental::filesystem; OR namespace fs = std::experimental::filesystem::v1; => ERROR: 'we want you to define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING'
how does one fix these errors?
Online, I found that adding these lines of code to your ProjectName.Build.cs file (which can be found and edited after clicking on the file in "solution explorer") instead of the original declaration for PCHUsage:
PCHUsage = PCHUsageMode.NoSharedPCHs; PrivatePCHHeaderFile = "Folder.h"; CppStandard = CppStandardVersion.Cpp17;
this did get rid of the Errors when using std::filesystem BUT caused wayyyy more problems, and seemed harmful. After looking through other websites / possible solutions, it seems people online don't know how to actually use the filesystem library in visual studio, I've been looking for working code for weeks.
if you want to try and use the std::experimental::filesystem or std::experimental::filesystem::v1, I tried #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING, but no dice.
I'm in the same boat as you man, and it looks like you didn't get much help from the previous comments. If we can't find a solution / video tutorial online, I have a plan B. Maybe we could try and write a c++17 project using some different software, or maybe we could write a program using a different software that could be launched instead of enabling c++17 into Visual Studio; for example, if you wanted to list the files in a directory, make an executable file in your preferred other software, and launch that executable / feed it arguments from your visual studio project. That's the best I got man. I hope that sparks some creativity or whatever, but I think it's a lot more helpful than what I've seen online. Good luck to you! and I need that luck too

Using Boost library in XCode

not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4

Linking error while using Armadillo and Lapack

my name is Ulrich and I just started writing my bachelor thesis, which includes a part where i write a program, which needs a Singular Value Decompostion.
At first i need to say, English is my 2nd language and I'm totally new to programming. So please help me with easy words ;)
After some research, I found that Armadillo offers a good SVD()-function, which is based on the Lapack library, so I downloaded them both and tried to install them correctly, and I think i failed at that part:
So here is the relevant parts of my written code:
#include <iostream>
#include <vector>
#include <random>
#include <time.h>
#include <math.h>
#include <fstream>
#include <cmath>
#include <sstream>
#include <armadillo>
using namespace std;
using namespace arma;
mat A(N,2*N+1, fill::zeros); //the zeroess will be replaced later, don't worry i know a SVD of zeroes is stupid ;)
mat U;
mat V;
vec S=svd(A);
The compilation error is:
undefined reference to 'dgesdd_'
What I use:
Code Blocks with MinGW compiler (Probably latest version; I installed it 3 days ago).
Armadillo Library 4.650.4
Lapack Library 3.5.0
Windows 8.1
What I have done:
I linked in the settings of Codeblocks the compiler with the Folder containing a lot of .header files from Armadillo (.../include/armadillo_bits/)
and i linked in the same way the Lapack folder with 4 .header files(.../lapacke/include/)
Furthermore the readme from armadillo said I shall comment out certain lines from a file called config.hpp
i looked through the whole file, but i think i don't have to uncomment anything. but just in case you need to see it, I can upload the file for you.
PS: while searching for a solution I came across this question which looked quite similar to my problem and seems to be easy fixable, but unfortunately I don't understand anything of it:
Question from stackoverflow
i hope you can help me with this,
Ulli

How do I include Boost.Interprocess and Boost.DateTime properly?

This is a really basic question because I am a C++ newbie. I want to use the Boost.Interprocess library, but am having trouble building it. I'm trying to follow these instructions, but it's not working for me. Here is what I have:
#define BOOST_DATE_TIME_NO_LIB
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
using namespace std;
int main() {
cout << "Hello, beautiful world!\n";
}
But I get this error:
boost_1_55_0\boost\date_time\gregorian_calendar.hpp(63) : fatal error C1083: Cannot open include file: 'boost/date_time/gregorian_calendar.ipp': No such file or directory
I know Boost is able to load properly, because I can get an example that uses #include <boost/lambda/lambda.hpp> to work just fine. It's just when I try to include the Boost.Interprocess library that I am having trouble. The cause is clearly because it's having trouble including the Boost.DateTime library properly, but according to the documentation (linked above) I should be able to get by without separately compiling Boost.DateTime if I define BOOST_DATE_TIME_NO_LIB, right?
What am I missing here?
You need to add it to the preprocessor
In VS go to - Project >> properties >> C/C++ >> Preprocessor in the 'Preprocessor Definitions' paste BOOST_DATE_TIME_NO_LIB.
You can download boost libraries here: https://www.boost.org/users/download/
After that, you can include them in your projects. Also, you can check this video on how to add boost libraries in eclipse IDE on Ubuntu: https://www.youtube.com/watch?v=gN8zrnWxFeI