extension(p) function in Boost Filesystem?

Hello I've just finally figured out how to use the boost library fully I can check if a path object set to a filename is a directory , is a regular file, I can even list out the directory contents using a directory_iterator and cout that to cmd

but what I have tried too figure out is extension(p) function

my idea was to be able to use this extension(p) to check if any file in a folder are a extension of .txt for example

If I have a folder named folder1

It has 6 files in it

each with its own file extension like

.txt
.vim
.obj
.png
.bmp
.mp3

and now I wanted to check if any one of those files is of extension .vim

how would I do that using boost or this function extension()

note
I may be wrong on what function to use in boost for this kind of thing.
Last edited on
use the string() function: p.extension().string() == ".vim "
I tried doing that and I got no results

if(p.extension().string() == ".txt")
{

cout << ".txt found" << endl;

}
1
2
3
4
if(extension(p).string() == ".txt")
{
	cout << ".txt found" << endl;
} 
I'm getting this error?

main.cpp:546:17: error: 'std::string' has no member named 'string'

this is my code


1
2
3
4
5
path p = ("folder name");
if(extension(p).string() == ".txt")
{
	cout << ".txt found" << endl;
}
Last edited on
have you tried
1
2
3
4
if(extension(p) == ".txt")
{
	cout << ".txt found" << endl;
}

?
:) yes since the error is

main.cpp: In function 'int main(int, char**)':
main.cpp:546:29: error: 'std::string' has no member named 'string'


Oh! I seee
Last edited on
hmm no output yet in cmd It can't be the path object since its got the folder name correctly named ? hmm I tried also p.extension() == ".txt" still no results

this is my current code

1
2
3
4
5
path p = ("textfolder");
if(extension(p) == ".txt")
{
	cout << ".txt found" << endl;
}
Last edited on
It doesn't look like textfolder has an file extension (directories usually don't).
yes your very right I'm able to list out a directory but I'm not able to check each string for .txt wording or the file extension it self directly this is my actual code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   path p = ("textfolder");
                  try
                  {
                     
 copy(directory_iterator(p), directory_iterator(),
                       ostream_iterator<directory_entry>(cout, "\n"));
 
               
                   
                 
                  }
                   
                   
                   catch (const filesystem_error& ex)
                  {
                 
                  cout << ex.what() << endl;
                  }
 
                    if(directory_entry(p) == ".txt")
                   {
                   
                    cout << ".txt found" << endl;
                   }
Last edited on
I think you should use the extension function on a path object to a file that has an extension. Something like:
1
2
3
4
5
path p("textfolder/abc.txt");
if(extension(p) == ".txt")
{
	cout << ".txt found" << endl;
}


Using iterator to test all the files in the directory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
path p("textfolder");
try
{
	for (directory_iterator it(p); it != directory_iterator(); ++it)
	{
		cout << *it << endl;
		if(extension(*it) == ".txt")
		{
			cout << ".txt found" << endl;
		}
	}
}
catch (const filesystem_error& ex)
{
	cout << ex.what() << endl;
}


I'm not at all sure this works because I have not tested and I usually don't use boost.
Last edited on
The 2nd option seems to work :D the big question now is how did you figure that out so I can learn to figure out my own problems on my own
Well, looking at your code, you have p = ... on line 1, setting p equal to the directory. You never modify p, so on line 20 you are trying to get the extension of the directory.

Obviously, if you want to look at each file...you'll need to look at each file.
Topic archived. No new replies allowed.