从文本文件中提取所有目录名称

Extract all directory names from a text file

我有一个文本文件,其中的文件名及其子目录名可以出现在任何随机位置。例如

input_file.txt

This is a text file. This line has a file name and location Folder1/file1.dat appearing here.

This is another line: Folder2/file2.txt

Here is yet another line with ../Folder3/Folder4/file3.doc filename and location.

这将在 Linux 系统上;因此是正斜杠。

我需要一个可以从此类文件中提取所有目录 names/locations 的 C++ 代码。在上面的示例中,要提取的字符串将是:

Folder1
Folder2
../Folder3/Folder4

鉴于输入文件的上述格式,我想算法应该是这样的:

  1. 遍历文件中的每一行,查看该行中是否有正斜杠 (/)。

  2. 如果在一行中找到正斜杠,则提取该行最后一次出现的正斜杠 (/) 与出现的最后一个 space 字符之间的子字符串在它之前。

我已经尝试了几种不同的方法,例如下面的方法,但恐怕无法正常工作。

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    using std::cout; using std::endl;
    unsigned first, last;

    if(argc < 2)
    {
        cout << "\nPlease give valid file name!"<<endl;
        exit(1);
    }

    std::string para_file_name = argv[1];      //The name of the input file.
    std::ifstream configfile(para_file_name);

    while (getline(configfile, line)) {
       if (line.find(" ")) {
          if (line.find(" ")!=std::string::npos) first = line.find(" ");
          if (line.find("/")!=std::string::npos) last = line.find("/");
          std::string DirName = line.substr (first,last-first);
          cout << " DirName = " << DirName << endl;
       }
    }

代码必须与 C++11 之前的版本兼容,并且不能使用像 Boost 这样花哨的外部库。请使用本机 C++。

也许有点矫枉过正,但你可以使用正则表达式。

#include <iostream>
#include <regex>
#include <string>

int main() {
  std::cmatch m;
  std::regex_match("This is another line: Folder2/file2.txt", m,
                   std::regex(".*?([^/ ]+/)+.*"));

  std::cout << m.str(1) << std::endl;
  return 0;
}

输出

Folder2/

不是最简洁的,但比 <regex> 更高效,并且适用于 C++98。

#include <cstdlib>  // exit
#include <fstream>  // fstream
#include <iostream> // cout
#include <sstream>  // istringstream
#include <string>   // getline

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        std::cout << "\nPlease give valid file name!\n";
        exit(1);
    }

    // Load the file in
    std::string line;
    std::fstream file(argv[1]);

    // For each line of file...
    while (std::getline(file, line))
    {
        std::istringstream iss(line);
        std::string word;
        char delim = ' ';

        // For each word of line...
        while (std::getline(iss, word, delim))
        {
            size_t pos = word.find_last_of('/');

            // Word includes '/'
            if (pos != std::string::npos)
            {
                std::string dir_name = word.substr(0, pos);

                std::cout << dir_name << "\n";
            }
        }
    }
}

输出

Folder1
Folder2
../Folder3/Folder4