LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2018, 12:02 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,895

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
c++ , gnu 5.5, gnu++17, filesystem header not there.


Wanting to get away from my C source and header I have modified and use to add files into a list to use, I added some c++ to that one so now it's a mix of C++ and C, but I am wanting to try and get as pure C++ as I can. Looking into C++ loading a directory of files to manipulate I found this code example I modded a little and it is using a header called <filesystem>.
Code:
#include <string>
#include <cstdlib>
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main(int argc, const char **argv)
{
  if (argc < 2)
  {
    std::cout<<"not qulified"<<std::endl;
    return EXIT_FAILURE;
  }
  
    std::string path = argv[1];
    for (auto & p : fs::directory_iterator(path))
        std::cout << p << std::endl;
    return 0;
}

then when I put that example to the test, I get a error.
Code:
$ g++ -std=gnu++17 -o filesme filenames.cpp
filenames.cpp:4:22: fatal error: filesystem: No such file or directory
compilation terminated.
looking up what does gnu 5.5 have.
Quote:
to enable GNU extensions in addition to C++17 features, add -std=gnu++17.
but it does not find it. so I'm thinking 14 is the highest I can use in gnu 5.5?

So I know what to look for in developing this little program I am starting, or am I doing something wrong here?

Wanting to not have to distro hope/boot on my system as Slack is 5.5 and the other one I have installed has a higher gnu version that might use 17 and its filesystem, ( I don't know I have not looked in to that) but I got other things running in slack that are not setup the same in that other distro that I could run there instead so I cannot 'muiti-task' while programming.
 
Old 03-27-2018, 01:03 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,895

Original Poster
Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
UPDATE:

Code:
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

using namespace boost::filesystem;

int main(int argc, char *argv[]) {
    path p(argc>1? argv[1] : ".");

    if(is_directory(p)) {
        std::cout << p << " is a directory containing:\n";

        for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))
            std::cout << entry << "\n";
    }
}
found this, so I think I'm going to try my hand at using boost. just need to mod it for certain file types and sub dir. and slap it into a function instead of main.
 
Old 03-27-2018, 01:05 PM   #3
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Use the boost library.
Code:
// g++ whatevah.cpp -lboost_system -lboost_filesystem

#include <iostream>
#include <boost/filesystem.hpp>

using boost::filesystem::directory_iterator;

int main()
{
    for (auto & i : directory_iterator("/")) {
        std::cout << i << "\n";
    }
    return 0;
}
Edit: You found that quickly.

From here: http://en.cppreference.com/w/cpp/experimental/fs
Quote:
This library is directly based on boost.filesystem, which is currently available on more compilers and platforms than this experimental technical specification.

Last edited by norobro; 03-27-2018 at 01:11 PM.
 
1 members found this post helpful.
Old 03-27-2018, 01:12 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,895

Original Poster
Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Quote:
Originally Posted by norobro View Post
Use the boost library.
Code:
// g++ whatevah.cpp -lboost_system -lboost_filesystem

#include <iostream>
#include <boost/filesystem.hpp>

using boost::filesystem::directory_iterator;

int main()
{
    for (auto & i : directory_iterator("/")) {
        std::cout << i << "\n";
    }
    return 0;
}
Edit: You found that quickly.

From here: http://en.cppreference.com/w/cpp/experimental/fs
we must have posted about the same time, I'm just a new post one above you. thanks!
 
Old 03-27-2018, 01:22 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,594

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
There no such thing as "gnu 5.5" Nonetheless C++ is cool to play with it.
 
Old 03-27-2018, 02:10 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,895

Original Poster
Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Quote:
Originally Posted by NevemTeve View Post
There no such thing as "gnu 5.5" Nonetheless C++ is cool to play with it.
sooorrrry didn't add the extra 0 (zero) at the end. or
did I say gnu? I meant gcc.
Code:
gcc version 5.5.0 (GCC)
anyways I gotter all figured out.

Code:
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace boost::filesystem;

struct recursive_directory_range
{
    typedef recursive_directory_iterator iterator;
    recursive_directory_range(path p) : p_(p) {}

    iterator begin() { return recursive_directory_iterator(p_); }
    iterator end() { return recursive_directory_iterator(); }

    path p_;
};

size_t checkFileExt(std::string extension)
{
	unsigned int count = 0;
	std::vector <std::string> extensions = {"jpg", "JPG", "png", "bmp", "jpeg" };
	
	while ( count < extensions.size() )
	{
		if (extension != extensions[count] )
		{
			count++;
			continue;
		}
		else
		{
		    return 1;
		}
	}
	return 0;
}
std::string getFileExt(const std::string& s) 
{
    size_t i = s.rfind('.', s.length());
   if (i != std::string::npos) 
   {
      std::string lext = (s.substr(i+1, s.length() - i));
      //remove double quotes off right side
      return (lext.erase(lext.size() - 1));
   }
   return("");
}


size_t loadFiles(std::vector<std::string> &mylist ,path p)
{

  for (auto it : recursive_directory_range(p))
  {
      //convert char to string
      std::stringstream newStringPath;
      newStringPath << it;
      std::string myStringPath = newStringPath.str();

    if (  checkFileExt(getFileExt(myStringPath)) == 1 )
    {
        mylist.push_back(myStringPath);
    }
 }
 return 0;
 }
 
 

int main(int argc, char *argv[]) {
  
  
    path p(argc>1? argv[1] : ".");
    
    std::vector<std::string> mylist;
    
    loadFiles(mylist,p);
    
  for (unsigned int a = 0; a < mylist.size(); a++)
      std::cout<<mylist[a]<<std::endl;

return 0;
}

//g++ -std=c++11 -Os -Wall -pedantic getfilenames.cpp  -lboost_system -lboost_filesystem
that part anyways....

Last edited by BW-userx; 03-27-2018 at 02:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] file name too long to be stored in a GNU multivolume header Thymox Linux - Software 5 11-11-2017 02:00 PM
Visibility of variables not declared in the header file (GNU C). stf92 Programming 15 01-08-2014 04:30 PM
LXer: Debian 7.0 GNU/Linux vs. GNU/kFreeBSD Benchmarks LXer Syndicated Linux News 0 06-11-2013 02:00 AM
LXer: GNU Grep & Sed: Fallout Within The GNU FSF Camp LXer Syndicated Linux News 0 12-24-2012 05:39 PM
GNU Make and GNU GCC DEBUG vs RELEASE compiler options aryan1 Programming 1 01-12-2010 12:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration