【发布时间】: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
鉴于输入文件的上述格式,我想算法应该是这样的:
浏览文件中的每一行,看看该行是否有正斜杠 (/)。
-
如果在一行中找到正斜杠,则提取该行中最后一次出现的正斜杠 (/) 与出现在它之前的最后一个空格字符之间的子字符串。
我尝试了几种不同的方法,如下所示,但恐怕无法让它发挥作用。
#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