Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4274560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:51:48+00:00 2026-05-21T07:51:48+00:00

I want to save the content of different files to a vector: Vector(0) =

  • 0

I want to save the content of different files to a vector:
Vector(0) = Content File1
Vector(1) = Content File2
…

Later on I need to read out from each index of this vector line by line (getline):

getline(Vector(0), string myString)

As I read on different sites, I can’t use vector<istream> myVector.

So how can I solve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:51:50+00:00Added an answer on May 21, 2026 at 7:51 am

    It depends on the size of the data you want to manipulate. My two samples has been tested.

    You can use a class which handles some raw pointers

    #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;
    }
    

    Or a std::vector< std::vector< std::string > >. That’s a basic solution for small files but it works.

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what I want: Download a webpage from a server > Save content in a
I have a number of different xml files that I need to edit from
I want to read and save some data from this rss feed to in
I want to save a matrix to a text file, so I can read
I want to show different content if a user is admin(2), or noarmal user(1).
I want to save the contents of my Flex application form into a word
I want save the username and password when user login, and I added the
i want to save a variable to be global variable on all screens i
I want to save values as an ArrayList of double in a file. Whenever
I want to save webpage in document directory with the images,css and javascripts etc..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.