We help IT Professionals succeed at work.
Get Started

ofstream seg fault

samj
samj asked
on
1,652 Views
Last Modified: 2013-12-14
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?

many thanks

class A
{
    std::map<std::string, std::ofstream*> m_os;
    std::map<std::string, myType> t4s;
    std::vector<string> file_names;
public:
    A(){}
    void A_Mthd();
};

A::A()
{
   void populate();
}

void A::populate()
{
  /* some code to fill file_names vector here */

  /* Populate the ostream pointers map. */
  for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
      {
        string f = "mydirectory/" + (*i);
        ofstream* p_of = new ofstream( f.c_str(), ios::app);
        m_os[*i] = p_of;
      }


void A::A_Mthd(){
 for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
   (*m_os[*i]) << t4s[*i].B_Mthd() << " " << endl;
}
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 2 Answers and 48 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE