我有一个文本文件,其中文件名及其子目录名可以出现在任何随机位置。例如:
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
鉴于输入文件的上述格式,我想算法应该是这样的:
遍历文件中的每一行,查看该行中是否在任何位置都有正斜杠(/)。
如果在一行中找到一个正斜杠,请提取该行中最后一次出现的正斜杠(/)和它之前出现的最后一个空格字符之间的子字符串。
恐怕我尝试了几种不同的方法,例如,但是无法正常工作。
#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