开发者

How to extract out a paths from a file?

I need to read a file which contains paths of other files,there types and other data about them. The file looks like,

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

I need to extract out only the path names of the files and save them in an array but I am stuck with it. Here's my code,

// 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 开发者_如何学Goa 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;
}

I am confused what to do next.Even if I use strstr() to find .txt whenever it comes how would I get the whole path of it?


Maybe you should take a look at boost filesystem library.

It provides the things you need.

This should be an example how it can work. It should compile although I did not try it.

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());
    }
}

And of course it is lacking all kind of error checks.


If you need only to extract the path and the file will always look like this, you can read the file line by line and use string::find to find the first occurrence of a space and create a substring off each entry.

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
}


What you want to accomplish is actually the example code demonstrating how to use string::find_last_of on cplusplus.com:

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;
}


If you want to get the whole path of a given file which is in the current directory, the following code does it for you, using boost of course:

#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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜