C++ 如何从文件中提取路径?
我需要读取一个包含其他文件路径、类型和其他相关数据的文件。 文件看起来像C++ 如何从文件中提取路径?,c++,string,file,C++,String,File,我需要读取一个包含其他文件路径、类型和其他相关数据的文件。 文件看起来像 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 In
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;
}
//将文件读入内存
#包括
#包括
使用名称空间std;
int main(){
整数长度;
char-str[600];
如果流是;
is.open(“test.txt”,ios::binary);
//获取文件的长度:
is.seekg(0,ios::end);
长度=is.tellg();
is.seekg(0,ios::beg);
//将数据作为块读取:
is.read(str,长度);
//**在文件中找到txt文件的路径并将其保存在数组中…卡在这里**
is.close();
返回0;
}
我不知道下一步该怎么做。即使我每次都使用strstr()查找.txt,我怎么才能得到它的整个路径?也许你应该看看 它提供你需要的东西 这应该是它如何工作的一个例子。虽然我没有试过,但它应该可以编译
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::filesystem::path p(“test.txt”);
boost::filesystem::path absolutePath=boost::filesystem::system_complete(p);
boost::filesystem::path workDir=absolutePath.parent_path();
std::矢量文件;
std::字符串行;
std::ifstream infle(“test.txt”,std::ios_base::in);
while(getline(infle,line,'\n'))
{
file.push_back(line.substr(0,line.find_first_of(“”));
}
std::矢量完整文件名;
对于(std::vector::const_迭代器iter=file.begin();iter!=file.end();++iter)
{
boost::filesystem::path newpath=workDir/boost::filesystem::path(*iter);
如果(!boost::filesystem::is_目录(newpath)&&boost::filesystem::exists(newpath))
{
fullFileNames.push_back(newpath.native().c_str());
}
}
当然,它缺少所有类型的错误检查。也许你应该看看 它提供你需要的东西 这应该是它如何工作的一个例子。虽然我没有试过,但它应该可以编译
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::filesystem::path p(“test.txt”);
boost::filesystem::path absolutePath=boost::filesystem::system_complete(p);
boost::filesystem::path workDir=absolutePath.parent_path();
std::矢量文件;
std::字符串行;
std::ifstream infle(“test.txt”,std::ios_base::in);
while(getline(infle,line,'\n'))
{
file.push_back(line.substr(0,line.find_first_of(“”));
}
std::矢量完整文件名;
对于(std::vector::const_迭代器iter=file.begin();iter!=file.end();++iter)
{
boost::filesystem::path newpath=workDir/boost::filesystem::path(*iter);
如果(!boost::filesystem::is_目录(newpath)&&boost::filesystem::exists(newpath))
{
fullFileNames.push_back(newpath.native().c_str());
}
}
当然,它缺少所有类型的错误检查。您想要完成的实际上是示例代码,演示如何在以下位置使用
string::find_last_of
:
void SplitFilename(常量字符串和str)
{
未找到尺寸;
cout您想要完成的实际上是示例代码,演示如何在以下位置使用string::find_last_of
:
void SplitFilename(常量字符串和str)
{
未找到尺寸;
cout如果您只需要提取路径,并且文件将始终是这样的,您可以逐行读取文件,并使用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
}
如果您只需要提取路径,并且文件将始终是这样的,则可以逐行读取文件,并使用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
}
如果您想获取当前目录中给定文件的整个路径,下面的代码当然会使用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;
}
#包括
#定义BOOST_文件系统_版本3
#包括
名称空间fs=boost::filesystem;
int main()
{
fs::path my_path(“test.txt”);
if(fs::is_regular_file(my_path))//健全性检查:文件存在并且是文件(不是目录)
{
fs::path wholePath=fs::absolute(我的路径);
std::cout如果您想要获取当前目录中给定文件的整个路径,下面的代码当然可以使用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;
}
#包括
#定义BOOST_文件系统_版本3
#包括
名称空间fs=boost::filesystem;
int main()
{
fs::path my_path(“test.txt”);
if(fs::is_regular_file(my_path))//健全性检查:文件存在并且是文件(不是目录)
{
fs::path wholePath=fs::absolute(我的路径);
std::你会在每一行的末尾都有文本文件吗?你能保证文件名不会嵌入换行符吗?文件名是“安全”的吗?只有字母数字和一些简单的符号吗?或者文件名可以有ascii控制字符吗?你必须找出某种机制来分离可能不明确的数据,我尽可能多的了解会有帮助。现在这个文件布局不是很直观,也不是很有帮助,包含所有这些文件的根目录是什么?你到底想做什么现在离我的脑海很遥远,也许有更好的解释?你会一直在每一行的末尾都有文本文件
吗?你保证不会吗hat文件名不会嵌入换行符?文件名是否“安全”——只包含字母数字和一些简单符号?或者文件名是否可以包含ascii控制字符?您必须找到某种机制来分离可能存在歧义的数据,并且尽可能多地了解这些数据会有所帮助。现在,这种文件布局并不是真正直观的,也没有任何帮助,包含所有这些文件的根目录是什么?您现在想做的事情与我的想法相去甚远,也许是一个更好的解释?在编辑问题之前,可以使用空格来标记文件路径的结尾。您可以将其更改为查找最后一个空格,但还需要删除其中的尾随空格