How do I add file paths as nodes to a tree or stack in C++

Dane Jackson

I have a project to search, rename or delete files and folders on a selected drive on the computer using a data structure(A tree, a stack, or a queue). My question is, how do I add file paths and directories as nodes in C++?

2785528

Comment in other answer suggests using one of the exec() functions. Then parsing and studying the output.

I approve of that idea, but I find it easier to use popen(). Each of the following examples are part of the Linux API, so the calls are c compatible and can be used directly by C++. I expect popen() will be available on other OS's.

To clarify,

1) popen() is a function call for your C++ code to invoke.

2) You will also need to create strings for your OS to generate the lists you want, and submit them to your invocation of popen(). The 1st parameter is the command string

3) in read mode, the output of your "ls -lsa " or "dir" command will be written into the output pipe of the spawned process, and your code will need to 'suck it in', I recommend capturing it to a std::stringstream.

4) after capture of the "dir -r" output, then parse and extract dir's and file names from the stringstream.


Examples of C++ access to popen:

FILE* m_pipe = nullptr; // popen return a FILE*


// use m_pipe to read from sub-process std::out
m_pipe = ::popen (m_cmd.c_str(), "r");  // read mode
//       ^^ because popen is not in a namespace


m_pipe = ::popen(m_cmd.c_str(), "w"); // write to sub-process std::in

int32_t pcloseStat = ::pclose(m_pipe);



{
   (void)memset(buff, 0, BUFF_SIZE);

   // Reads characters from stream and stores them as a C string
   // into buff until
   //    a) (BUFF_SIZE-1) characters have been read or
   //    b) a newline or
   //    c) the end-of-file is reached,
   // whichever happens first.
   char* stat = fgets (buff, BUFF_SIZE, m_pipe); // returns buff or null
   int myErrno = errno;               //^^^^^^ -- created by popen
}

Example of building a linux command for popen 1st parameter ...

  std::string md5sumCmd ("head --bytes=1M " + mPFN +" | md5sum");

This command grabs the 1st 1Megabyte of file name in mPFN (a std::string), and pipes that output into md5sum ... essentially generating an md5sum of the 1s Meg of the file. The md5sum output is what will be received by the calling process.


You will need to create appropriate commands (to pass to popen) to show dir's and folder's and file names, etc.

What ever works from the command line should be fine, but some options might make parsing the output easier.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

GIT: How do I add a file to the first commit (and rewrite history in the process)?

来自分类Dev

How do I iterate over a file?

来自分类Dev

Passing File with intent, how do i retrieve it

来自分类Dev

How do i write the integers that in this file to the mynumbers.txt file?

来自分类Dev

How do I add a close button to a tab of the MahApps TabControl?

来自分类Dev

How do I add a Web URL to a document using LotusScript

来自分类Dev

How do I add each price to the current price?

来自分类Dev

Office 365 Add-In - How do I hide the task pane?

来自分类Dev

How do I add additional hosts to HDP cluster

来自分类Dev

How do I add percentage signs to an axis in barplot in R?

来自分类Dev

How do you I add a module into my PYTHONPATH?

来自分类Dev

How do i remove NULL input in C?

来自分类Dev

How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests?

来自分类Dev

How do I update the working tree when checking out a branch with libgit2?

来自分类Dev

How do I create a link to a pdf file with rails?

来自分类Dev

How do I find the path of a data file in Dancer to use in Javascript?

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

How do I open a VBA file in notepad or notepad++

来自分类Dev

How to do a "tree walk" recursively on an Abstract Syntax Tree?

来自分类Dev

How do paths work in a Java deployed REST web service?

来自分类Dev

git how to do diff outside of working tree

来自分类Dev

How do I support generic expression in C# 2.0

来自分类Dev

How do I use an extension function in another class? C#

来自分类Dev

how do I convert a double array in C to python list?

来自分类Dev

How do I effectively access list information in C++?

来自分类Dev

How do I add a click event to a directed graph using dagre d3.js(javascript library)?

来自分类Dev

How do I add an icon to the taskbar for a simple win32 dialog?

来自分类Dev

How to pipe gdb's full stack trace to a file?

来自分类Dev

Depth first search list paths to all end nodes

Related 相关文章

  1. 1

    GIT: How do I add a file to the first commit (and rewrite history in the process)?

  2. 2

    How do I iterate over a file?

  3. 3

    Passing File with intent, how do i retrieve it

  4. 4

    How do i write the integers that in this file to the mynumbers.txt file?

  5. 5

    How do I add a close button to a tab of the MahApps TabControl?

  6. 6

    How do I add a Web URL to a document using LotusScript

  7. 7

    How do I add each price to the current price?

  8. 8

    Office 365 Add-In - How do I hide the task pane?

  9. 9

    How do I add additional hosts to HDP cluster

  10. 10

    How do I add percentage signs to an axis in barplot in R?

  11. 11

    How do you I add a module into my PYTHONPATH?

  12. 12

    How do i remove NULL input in C?

  13. 13

    How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests?

  14. 14

    How do I update the working tree when checking out a branch with libgit2?

  15. 15

    How do I create a link to a pdf file with rails?

  16. 16

    How do I find the path of a data file in Dancer to use in Javascript?

  17. 17

    How do I get Ruby to search for a pattern on the tail of a local file?

  18. 18

    How do I open a VBA file in notepad or notepad++

  19. 19

    How to do a "tree walk" recursively on an Abstract Syntax Tree?

  20. 20

    How do paths work in a Java deployed REST web service?

  21. 21

    git how to do diff outside of working tree

  22. 22

    How do I support generic expression in C# 2.0

  23. 23

    How do I use an extension function in another class? C#

  24. 24

    how do I convert a double array in C to python list?

  25. 25

    How do I effectively access list information in C++?

  26. 26

    How do I add a click event to a directed graph using dagre d3.js(javascript library)?

  27. 27

    How do I add an icon to the taskbar for a simple win32 dialog?

  28. 28

    How to pipe gdb's full stack trace to a file?

  29. 29

    Depth first search list paths to all end nodes

热门标签

归档