如何将不同文件的内容写入矢量以供getline进一步使用
How to write the content of different files to a vector for further use with getline
我想把不同文件的内容保存到一个矢量:向量(0)=内容文件1向量(1)=内容文件2…
之后,我需要从这个向量的每个索引逐行读出(getline):
getline(Vector(0), string myString)
由于我在不同的网站上阅读,我不能使用vector<istream> myVector
。
那我怎么解呢?
这取决于您想要操作的数据的大小。我的两个样品已经测试过了。
你可以使用一个类来处理一些原始指针
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
class file_vector
{
public:
file_vector()
{}
virtual ~file_vector()
{
for( std::vector< std::ifstream* >::iterator it = m_file_streams.begin(); it != m_file_streams.begin(); it++)
{
std::ifstream * p_stream = *it;
delete p_stream;
}
}
void append_file(const std::string& file_name)
{
std::ifstream * p_stream = new std::ifstream( file_name.c_str() );
if(p_stream->is_open())
m_file_streams.push_back(p_stream);
else
delete p_stream;
}
void reset()
{
for( std::vector< std::ifstream* >::iterator it = m_file_streams.begin(); it != m_file_streams.end(); it++)
{
std::ifstream * p_stream = *it;
p_stream->seekg(0,p_stream->beg);
}
}
size_t size()
{
return m_file_streams.size();
}
std::ifstream & get(size_t index)
{
return * m_file_streams.at(index); // Using at because of index check
}
std::ifstream & operator [] (size_t index)
{
return get(index);
}
private:
std::vector< std::ifstream* > m_file_streams;
};
int main()
{
file_vector files_content;
files_content.append_file("file1.txt");
files_content.append_file("file2.txt");
files_content.append_file("file3.txt");
for(size_t i = 0; i < files_content.size(); i++)
{
std::string current_line;
while(std::getline(files_content[i],current_line))
std::cout << current_line << std::endl;
}
files_content.reset(); // To make getline usable again
return 0;
}
或std::vector
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
typedef std::vector< std::string > string_vec;
typedef std::vector< std::string >::iterator string_it;
typedef std::vector< string_vec> file_vec;
typedef std::vector< string_vec >::iterator file_it;
int main()
{
string_vec file_names;
file_names.push_back("file1.txt");
file_names.push_back("file2.txt");
file_names.push_back("file3.txt");
file_vec files_content;
string_vec empty_file_content;
for(string_it file_name = file_names.begin(); file_name != file_names.end(); file_name++)
{
std::ifstream input_stream( file_name->c_str() );
files_content.push_back(empty_file_content);
if(input_stream.is_open())
{
string_vec & current_file_content = files_content[ files_content.size() - 1 ];
std::string current_line;
while(std::getline(input_stream, current_line))
current_file_content.push_back(current_line);
}
}
// Some stuff
// Reading the content later on
for(file_it file = files_content.begin(); file != files_content.end(); file++)
{
for(string_it line = file->begin(); line != file->end(); line++)
{
std::cout << *line << std::endl;
}
}
return 0;
}
iostream不能放入STD容器中。
也许你可以用一个void指针数组来保存它们
要清楚,您是想存储文件句柄,然后从文件中读取,还是将文件读取到类似容器的字符串中并将其存储在vector中?
如果您谈论的是后者,并且文件是文本文件,那么应该可以将它们读入std::string
对象并以该形式存储它们。在std::string
中存储回车等没有问题,但请记住,这种解决方案可能无法扩展:如果您需要在矢量中存储4Gb+或100,000个文件,会发生什么?
如果要存储流的vector对象,可以考虑使用指针容器或智能指针的vector对象,如下所示:
typedef boost::shared_ptr<std::istream> stream_ptr
std::vector<stream_ptr> stream_container;
// Then add your streams
stream_container.push_back(stream_ptr(new std::ifstream("f1")));
stream_container.push_back(stream_ptr(new std::ifstream("f2")));
stream_container.push_back(stream_ptr(new std::ifstream("f3")));
:
// Then iterate through and call get line..
std::vector<stream_ptr>::iterator it(stream_container.begin()), end(stream_container.end());
std::string sv;
for(; it != end; ++it)
getline(**it, sv);
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 当用户键入分隔符时,停止getline()输入
- getline() 的原型/库是什么;
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 错误:调用'getline'没有匹配函数
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 将 getline() 与文件一起使用
- 我有三个 getline,但是一旦编译,输入就太多了
- 在 std::getline 和 std::cin 期间卡在循环中
- 为什么 getline() 不读取文本文件中的所有内容?
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- std::getline没有在while循环中重新请求用户输入
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- Getline(cin,str)的奇怪输出
- 我在C++中使用 getline() 函数时遇到问题
- 而循环跳过 cin.getline() 对于 C 字符串
- 如何将不同文件的内容写入矢量以供getline进一步使用