【问题标题】:How to extract out a paths from a file?如何从文件中提取路径?
【发布时间】:2011-04-04 11:42:58
【问题描述】:

我需要读取一个文件,其中包含其他文件的路径、类型和有关它们的其他数据。 该文件看起来像,

LIST OF SUB DIRECTORIES:
Advanced System Optimizer 3
ashar wedding and home pics
components
Documents and Settings
khurram bhai
media
new songs
Office10
Osama
Program Files
RECYCLER
res
Stationery
System Volume Information
Templates
WINDOWS



LIST OF FILES:
.docx  74421
b.com  135168
ChromeSetup.exe  567648
Full & final.CPP  25884
hgfhfh.jpg  8837
hiberfil.sys  267964416
myfile.txt.txt  0
pagefile.sys  402653184
Shortcut to 3? Floppy (A).lnk  129
Thumbs.db  9216
vcsetup.exe  2728440
wlsetup-web.exe  1247056

我只需要提取文件的路径名并将它们保存在一个数组中,但我坚持下去。这是我的代码,

// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char str[600];

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);


  // read data as a block:
  is.read (str,length);
  //**find the path of txt files in the file and save it in an array...Stuck here**
  is.close();
  return 0;
}

我很困惑下一步该怎么做。即使我使用 strstr() 来查找 .txt ,我如何获得它的整个路径?

【问题讨论】:

  • 你会总是在每一行的末尾都有Text file吗?您是否保证文件名不会嵌入换行符?文件名是否“安全”——只有字母数字和一些简单的符号?或者文件名可以有 ascii 控制字符吗?您必须找出一些机制来分解可能不明确的数据,并且尽可能多地了解会有所帮助。
  • 现在这个文件布局不是很直观或远程帮助,包含所有这些文件的根目录是什么?你到底想要做什么现在离我的想法很远,也许有更好的解释?

标签: c++ string file


【解决方案1】:

也许你应该看看boost filesystem library

它提供了你需要的东西。

这应该是它如何工作的一个例子。虽然我没有尝试过,但它应该可以编译。

boost::filesystem::path p("test.txt");
boost::filesystem::path absolutePath = boost::filesystem::system_complete(p);
boost::filesystem::path workDir = absolutePath.parent_path();

std::vector<std::string> file;
std::string line;
std::ifstream infile ("test.txt", std::ios_base::in);
while (getline(infile, line, '\n'))
{
    file.push_back (line.substr(0, line.find_first_of(" ")));
}

std::vector<std::wstring> fullFileNames;
for(std::vector<std::string>::const_iterator iter = file.begin(); iter != file.end(); ++iter)
{
    boost::filesystem::path newpath= workDir / boost::filesystem::path(*iter);
    if(!boost::filesystem::is_directory(newpath) && boost::filesystem::exists(newpath))
    {
        fullFileNames.push_back(newpath.native().c_str());
    }
}

当然,它缺少各种错误检查。

【讨论】:

  • 问题是关于解析文本文件中的文件名,而不是解析文件名本身。
  • 仍然使用 boost 使这项任务变得微不足道,文档中有示例。
  • 同意,但如果文件名包含空格,您应该使用 find_last_of(" ")。
  • 这是一个例子。我更喜欢unix世界,根本不在文件名中使用空格。但我同意这个搜索需要改进。但我把这留给 OP。
【解决方案2】:

如果您只需要提取路径并且文件将始终看起来像这样,您可以逐行读取文件并使用string::find查找第一次出现的空格并创建每个条目的子字符串。

size_t index = str.find(" ");
if(index != string::npos) // sanity checing
{
   string path = str.substr(0, index);
   //do whatever you want to do with the file path
}

【讨论】:

  • 在您编辑问题之前,可以使用空格来标记文件路径的结尾。
  • 您可以将其更改为查找最后一个空格,但您还需要删除结果中的尾随空格。
【解决方案3】:

您想要完成的实际上是演示如何在cplusplus.com 上使用string::find_last_of 的示例代码:

void SplitFilename (const string& str)
{
  size_t found;
  cout << "Splitting: " << str << endl;
  found=str.find_last_of("/\\");
  cout << " folder: " << str.substr(0,found) << endl;
  cout << " file: " << str.substr(found+1) << endl;
}

【讨论】:

    【解决方案4】:

    如果您想获取当前目录中给定文件的完整路径,以下代码会为您完成,当然使用 boost:

    #include <iostream>
    
    #define BOOST_FILESYSTEM_VERSION 3
    #include <boost/filesystem.hpp>
    namespace fs = boost::filesystem;
    
    int main()
    {
      fs::path my_path("test.txt");
      if(fs::is_regular_file(my_path)) // Sanity check: the file exists and is a file (not a directory)
      {
        fs::path wholePath = fs::absolute(my_path);
        std::cout << wholePath.string() << std::endl;
      }
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2011-04-28
      相关资源
      最近更新 更多