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

  • Home
  • SEARCH
  • 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 3967568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:44:06+00:00 2026-05-20T03:44:06+00:00

In the following code, I’m having some sort of issue getting my .yaml file

  • 0

In the following code, I’m having some sort of issue getting my .yaml file parsed using parser.GetNextDocument(doc);. After much gross debugging, I’ve found that the (main) issue here is that my for loop is not running, due to doc.size() == 0; What am I doing wrong?

void
BookView::load()
{
  aBook.clear();

  QString fileName =
    QFileDialog::getOpenFileName(this, tr("Load Address Book"),
                 "", tr("Address Book (*.yaml);;All Files (*)"));
  if(fileName.isEmpty())
    {
      return;
    }
  else
    {
      try
    {
      std::ifstream fin(fileName.toStdString().c_str());
      YAML::Parser parser(fin);
      YAML::Node doc;
      std::map< std::string, std::string > entry;

      parser.GetNextDocument(doc);
      std::cout << doc.size();

      for( YAML::Iterator it = doc.begin(); it != doc.end(); it++  )
        {
          *it >> entry;
          aBook.push_back(entry);
        }

    }
      catch(YAML::ParserException &e)
    {
      std::cout << "YAML Exception caught: "
            << e.what()
            << std::endl;
    }
    }
  updateLayout( Navigating );
}

The .yaml file being read was generated using yaml-cpp, so I assume it is correctly formed YAML, but just in case, here’s the file anyways.

^@^@^@\230---
-
  address: ******************
  comment: None.
  email: andrew(dot)levenson(at)gmail(dot)com
  name: Andrew Levenson
  phone: **********^@

Edit: By request, the emitting code:

void
BookView::save()
{
  QString fileName =
    QFileDialog::getSaveFileName(this, tr("Save Address Book"), "",
                 tr("Address Book (*.yaml);;All Files (*)"));
  if (fileName.isEmpty())
    {
      return;
    }
  else
    {
      QFile file(fileName);
      if(!file.open(QIODevice::WriteOnly))
    {
      QMessageBox::information(this, tr("Unable to open file"),
                   file.errorString());
      return;
    }

      std::vector< std::map< std::string, std::string > >::iterator itr;
      std::map< std::string, std::string >::iterator mItr;
      YAML::Emitter yaml;

      yaml << YAML::BeginSeq;
      for( itr = aBook.begin(); itr < aBook.end(); itr++ )
    {
      yaml << YAML::BeginMap;
      for( mItr = (*itr).begin(); mItr != (*itr).end(); mItr++ )
        {
          yaml << YAML::Key << (*mItr).first << YAML::Value << (*mItr).second;
        }
      yaml << YAML::EndMap;
    }
      yaml << YAML::EndSeq;

      QDataStream out(&file);
      out.setVersion(QDataStream::Qt_4_5);
      out << yaml.c_str();
    }      
}
  • 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-20T03:44:07+00:00Added an answer on May 20, 2026 at 3:44 am

    Along the lines of what you thought, the problem is that you’re writing using QDataStream but reading using plain std::ifstream. You need to do either one or the other.

    If you want to use the QDataStream, you’ll need to read it in as well. Check out the doc for more detail, but it looks like you can just grab the YAML string:

    QDataStream in(&file);
    QString str;
    in >> str;
    

    and then pass it to yaml-cpp:

    std::stringstream stream; // remember to include <sstream>
    stream << str; // or str.toStdString() - I'm not sure about how QString works
    YAML::Parser parser(stream);
    // etc.
    

    The point of a std::stringstream is to transform your string containing YAML into a stream that the YAML parser can read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.