Question:
How can i find a folder name in C++?
Harry
2012-08-01 21:04:15 UTC
I need to find a specific folder name of a folder in C++, i know where the folder will always be on any computer but im still new to programming so i just dont know how to find it with a program. The folder itself will always be in the same place but its name will never be the same unless on a different computer. So pretty much find a folder name and put it into a string. If theres a tutorial on this for new people then great ill read/watch it but if not any help would be appreciated :)
Three answers:
2012-08-01 21:29:09 UTC
You've hit upon an interesting problem. Standard C++ does not have any comprehension of directories. It has file-handing capability, but can't natively handle directory manipulation in a platform-independent, portable fashion.



If you're using the Win32 API, there Windows-specific functions you can call ( FindFirstFile() and FindNextFile() ) that will let you traverse the contents of a directory. You can then inspect the attributes of each found file to see if it's a directory or not. Take a look at the following link on MSDN for some example code:



http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx



For other platforms, you use either the Boost or Dirent libraries to accomplish your goal. They're not quite standard C++, but they're close, and generally platform-portable. Dirent is the C Posix library facilitating directory manipulation, while Boost is a much larger collection of libraries. I'd recommend trying Dirent first - it's lighter-weight and easier to use.



Here's some sample code that shows the contents of a directory (in this case, the current directory):



#include

#include

#include

#include

int main()

{

DIR *pdir;

struct dirent *pent;

pdir=opendir("."); //"." refers to the current dir

if (!pdir){

printf ("opendir() failure; terminating");

exit(1);

}

errno=0;

while ((pent=readdir(pdir))){

printf("%s", pent->d_name);

}

if (errno){

printf ("readdir() failure; terminating");

exit(1);

}

closedir(pdir);

}





Hope that helps a bit;



Wire
Ethan
2017-01-20 05:22:16 UTC
1
ʄaçade
2012-08-01 21:26:36 UTC
The key to that is the fact that you need to run an operating system command (shell command) and read its stdout. For example:



char cmd[1024];

strcpy (cmd, "/bin/ls");

sream = popen (cmd.c_str(), "r"); // Run the ls command.



Now read the lines from the stream like you would a text file. Then

close (stream);

when you have read all the output from ls.



So once you have that working, you can change the "ls" command to something more suitable for your task. The "find" command might work well for you. It will search a file tree for the named file (pattern)



Popen, by the way, means "pipe open", and it executes the shell command into a readable pipe. Treat like a temporary text file.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...