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 9199801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:37:08+00:00 2026-06-17T22:37:08+00:00

I am new to C++. I am having trouble outputting data to a file.

  • 0

I am new to C++. I am having trouble outputting data to a file. I am using an iterator to print out a map. The print method takes i, a key value, and prints out it’s corresponding vector. Now, this works perfectly fine when I output normally using cout<< but when I try to put the same output into a file, my program crashes. I know it is the *it that in the outfile<< line that is crashing it, because if I replace it with some random string it outputs it to the file fine. Also, I know the parameters in the print method are not causing any problems, because I can transfer that method directly to the main function of the program and get the same error. Any help would be greatly appreciated on how to fix this, thank you!
Here is my print method where the error is happening:

    public: void print(int i, vector<string> in, ostream& outfile) // print method for printing a vector and it's key
{

    sort(in.begin(), in.end()); // sort the vector alphabetically first

    vector<string>::iterator it; 

    it= unique(in.begin(), in.end()); // makes sure there are no duplicate strings

    in.resize( distance(in.begin(),it) );

    for( it = in.begin(); it != in.end(); it++ ) // iterate through it

    cout << i << ": "<< *it<<endl; // and print out the key value and each string in the vector
   // outfile<< i << ":" << *it<< endl; // prints to file
}
  • 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-06-17T22:37:09+00:00Added an answer on June 17, 2026 at 10:37 pm

    Are you using the cout line at the same time? If so, I think I know what it is.

    The for loop, without braces will execute the next statement as its loop body. If you use both the cout line and the outfile line, you will print everything, then after the loop, it will be located just past the end of the array. You then try and dereference this and write it to a file, which of course fails since you’re dereferencing an invalid iterator.

    Short answer, wrap the statements in your for loop with braces.

    For example, you have the following (when indented properly):

    for( it = in.begin(); it != in.end(); it++ ) // iterate through it
        cout << i << ": "<< *it<<endl; 
    outfile<< i << ":" << *it<< endl; // prints to file
    

    On the last line of that, it = in.end(), where in.end() is the element just past the end of the vector. You then try and access the element at that location which doesn’t exist (and is invalid), and hence it fails. Instead, you need to move that inside the loop, which should read

    for( it = in.begin(); it != in.end(); it++ ) // iterate through it
    {
        cout << i << ": "<< *it<<endl; // and print out the key value and each string in the vector
        outfile<< i << ":" << *it<< endl; // prints to file
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble creating a new edmx file using Visual Studio 2011 beta
I'm having trouble trying to update my xml file with a new value. I
I'm having trouble using the new async/await tools in c#. Here is my scenario:
I am new to rails and I having trouble using cucumber to run my
I'm brand new to ruby and rails and I'm having trouble creating a sign-out
I use jQuery UI's new Tooltip and having trouble with figuring out how to
I'm having trouble creating a new model row in the database using ActiveRecord in
I'm having trouble figuring out how to create new entries in a table and
im pretty new to extjs and having trouble with it i am using the
I am having trouble figuring this out. Facebook is implementing a new policy https://developers.facebook.com/roadmap/offline-access-removal/

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.