Hello everybody !
I been trying for hours to figure out how to use countLine in this code. It's a vector class and it use the raw pointers technique which is all new to me. For a simple vector I can use this:
... but nothing I tried worked in this class of vector. Could someone show me how to get the lineCount (Element Count) to work. Thanks in advance.Code:cout << v_DATA.size();
Source found here: c++ - How to write the content of different files to a vector for further use with getline - Stack Overflow
Code:// ... You can use a class which handles some raw pointers (For Large Files)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
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
cin.get();
return 0;
}