Dec 23, 2012 at 3:05pm UTC
hello
how to stream next directory or folder in linux ?
for example :
if direct /home/member/ exist
then get all of next directory
>/home/member/user1 /data.txt
>/home/member/user2 /data.txt
>/home/member/user3 /data.txt
and if data.txt will break
then i want display word of "user1,user2,user3" on console
please help me ^^
thanks you ^^
Last edited on Dec 23, 2012 at 3:08pm UTC
Dec 23, 2012 at 3:32pm UTC
Use opendir() and readdir() in *nix world.
Dec 25, 2012 at 1:49pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <cstring>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
int main ()
{
char n[20];
unsigned char isFolder = 0x4;
unsigned char isFile = 0x8;
DIR *dir;
struct dirent *ent;
dir = opendir ("User/" );
if (dir != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
//printf ("%s\n", ent->d_name);
//folder sign
if (ent->d_type == isFolder)
{
cout <<ent->d_name <<"\n" ;
}
}
closedir (dir);
} else {
/* could not open directory */
perror ("" );
return 0;
}
cout << "=========" << endl;
}
out put:
1 2 3 4 5 6 7 8 9 10
user2
..
user3
user1
.
======
Process returned 0 (0x0) execution time : 0.012 s
Press ENTER to continue .
i got a little problem
how to make output without any dot ?
".."
"."
and make output like this
and under "====" arranged alphabetically
and also go to next folder for read data.txt
1 2 3 4 5 6 7 8 9 10 11
user2
user3
user1
======
user1
user2
user3
thanks you
sorry for my bad english :D
Last edited on Dec 25, 2012 at 1:52pm UTC
Dec 25, 2012 at 5:03pm UTC
boost skips those by default, and has the advantage of being portable to other platforms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main ()
{
std::vector<std::string> v;
fs::path p("User" );
if (exists(p) && is_directory(p))
for (auto i = fs::directory_iterator(p); i != fs::directory_iterator(); ++i)
if (is_directory(i->path()))
v.push_back(i->path().filename().string());
for (auto & s: v)
std::cout << s << '\n' ;
std::cout << "=========\n" ;
sort(v.begin(), v.end());
for (auto & s: v)
std::cout << s << '\n' ;
}
Last edited on Dec 25, 2012 at 5:04pm UTC
Dec 25, 2012 at 11:39pm UTC
@cubbi how to get the boost header ?
.....
i'v seen the boost file , it's too large :D
Last edited on Dec 25, 2012 at 11:42pm UTC