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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:59:48+00:00 2026-06-18T11:59:48+00:00

I was wondering how I would go about splitting a text file, chunk by

  • 0

I was wondering how I would go about splitting a text file, chunk by chunk, and storing groups of lines as a single string.. For example:

I have a text file of questions, some of which are multiple lines. After a variable number of lines (depending on how many lines the question takes up) there is a blank line, then the answer, followed by another question (which could also be longer than 1 line), blank line, answer.

Something like this, where "q" are lines that should be stored as a single string and "a" should also be a single string:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaa

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

I tried reading line by line, combining string line + line if line != "". But it got confusing and messy and I couldn’t ever get it to work correctly.

I simply want to store the first set of q‘s as a single string and put it in vector[0] and the first set of a‘s in vector[1]. The second set of q‘s in vector[2]. The second set of a‘s in vector[3].. and so on. Both the q’s and the a’s can be several lines.


Any suggestions or help will be much appreciated!

#include <vector>
#include <fstream>
#include <iostream>
#include "Question.h"
#include <iomanip>

using namespace std;

int main(int argc, char * argv[]){
    ifstream infile;
    string filename = "questions.txt";//manually set for testing.

    //cout<<"Enter the questions file: ";
    //cin>>filename;

    infile.open(filename.c_str());

    if (!infile){
            cout<<"error"<<endl;
            return 0;
    }
    else {
            cout<<"file opened!"<<endl;
    }
    vector<string> myvector;

    string line;
    string additionalLine;
    int totalLines = 0;
    while(getline(infile,line)){
            totalLines++;
    }
    cout<<"total lines: "<<totalLines<<endl;

/*
    while(getline(infile,line,'\n')){
            cout<<line<<endl;
    }
*/

    while(getline(infile,line,\n)){
            if (line == ""){
                    cout<<"empty"<<endl;
            }

            else {
                    cout<<"line is not empty"<<endl;
                    additionalLine = additionalLine + line;
            }
            if (line != ""){
                    myvector.push_back(additionalLine);
            }

    }

    for(int i=0; i < (myvector.size()); i++){
            cout<<myvector[i]<<endl;
    }

    //TESTING
    cout<<"Question: "<<endl;
    cout<<myvector[0]<<endl;
    cout<<"Answer: "<<endl;
    cout<<myvector[1]<<endl;

    return 0;

}

  • 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-18T11:59:50+00:00Added an answer on June 18, 2026 at 11:59 am

    I tweaked the code a bit and got it to work!

    I changed the way I approached cycling through the lines of the text file, changing it to while (!infile.eof()) and manually got the lines. I also added a statement to detect if there was a new line. I also had to reposition where I reset the string variables back to “”.

    Thanks for the suggestions! Heres the solution code:

    #include <vector>
    #include <fstream>
    #include <iostream>
    #include "Question.h"
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char * argv[]){
        ifstream infile;
        string filename = "questions.txt";//manually set for testing.
    
        //cout<<"Enter the questions file: ";
        //cin>>filename;
    
        infile.open(filename.c_str());
    
        if (!infile){
                cout<<"error"<<endl;
                return 0;
        }
        else {
                cout<<"file opened!"<<endl;
        }
        vector<string> myVec;
    
        string line;
        string comboLine="";
    
        while(!infile.eof()){
                getline(infile,line);
    
                if (line == "" || line == "\0")  {
                        //cout<<"->BLANK LINE DETECTED<-"<<endl;
                        myVec.push_back(comboLine);
                        comboLine="";
                }else {
                        comboLine = comboLine + line;
                        //cout<<"comb: "<<comboLine<<endl;
                }
    
                line = "";
    
        }
        infile.close();
    
        //TESTING
        cout<<"Question: "<<endl;
        cout<<myVec[0]<<endl;
        cout<<"Answer: "<<endl;
        cout<<myVec[1]<<endl;
        cout<<"Question 2: "<<endl<<myVec[2]<<endl;
        cout<<"Answer 2: "<<endl<<myVec[3]<<endl;
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how I would go about externalizing a proc (example below) so
Just wondering how people would go about this. Say I have one class that
I am wondering how I would go about storing this in MySQL I am
I have a SplitContainer and was wondering how I would go about setting a
I'm just wondering how I would go about splitting a pixel array (R,G,B,A,R,G,B,A,R,G,B,A,etc.) into
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
I'm wondering how you would go about designing a good permgen space string in
I was wondering how one would go about developing a website from scratch with
I'm wondering how i would go about making the following application: a user signs
I am wondering how I would go about performing a certain function if a

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.