简体   繁体   中英

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

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++?

For your node based structures, add a string property that would serve as your file path. You might need to replace "\\" with "/" in it however as the forward slash is often an escape character in most languages. For example in a queue:

class Node {
Node next;
char[50] path;
}

And you can create accessors and mutators the same way you would anything else in a class. This will allow you to assign it values and to read the values. Folders could be used as a parent and the files are children. A tree structure would likely be the easiest way to do this.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM