【问题标题】:Extract all directory names from a text file从文本文件中提取所有目录名称
【发布时间】:2020-04-02 18:22:23
【问题描述】:

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

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 系统上;因此是正斜杠。

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

Folder1
Folder2
../Folder3/Folder4

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

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

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

我尝试了几种不同的方法,如下所示,但恐怕无法让它发挥作用。

#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++。

【问题讨论】:

  • “我已经尝试了几种不同的方法”请为其中一种显示minimal reproducible example,然后我们可以帮助您解决它
  • 分割线有一个解决方案here。要查找斜线和空格,您可以使用find_last_of,然后使用substr 进行剪切
  • @idclev463035818 OK 添加了我尝试过的解决方案。我应该说,我不是来自 C++ 背景,尽管有其他高级语言的经验。所以我不是想把它用作一种代码编写服务,而是真正地尝试学习和解决我遇到的问题。
  • minimal reproducible example 的一部分应该是编译器错误以及预期和实际输出。您没有声明 configfile 并且在代码中您没有打开任何文件

标签: c++ text-processing


【解决方案1】:

不是最简洁的,但比 &lt;regex&gt; 性能更高,并且可以与 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

【讨论】:

    【解决方案2】:

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

    #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/
    

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2023-01-04
      相关资源
      最近更新 更多