All text file into one string

Hello. I have a question concerning using getline to get all of the text's content into one string. I've written a text file outside the program and when I wanted to put all of the sentences into one string I only got the last sentence. I put it like this:

------------
Hello! Welcome to the program.

I want to welcome you to explore the features of this program to your heart's content. This program is capable of doing many amazing things that would've been difficult to do by hands.

As of now I'm still adding features to this program so I'm continuously updating this program so that it will be so much better.

When I ran the program I only got the last sentence. How do I get all of the sentences into one string variable? I don't want to split them up. I want all of them in one string so that I can easily output all of it.

Any suggestions?
Something like this may work:


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr="Hello! Welcome to the program.\n I want to welcome you to explore the features of this program to your hearts content. This program is capable of doing many amazing things that wouldve been difficult to do by hands.\n As of now Im still adding features to this program so Im continuously updating this program so that it will be so much better.";
  cout << mystr << "\n";
  return 0;
}
Last edited on
What I have in mind is:

#include <iostream>
#include <string>
#include <fstream>

void main()
{
string text;
istream text_input;
text_input("test_text.txt")
if (text_input.fail())
{
cout<<"Error! Terminating program.\n";
return;
}
else
{
getline(text_input, text);
}
cout<<text<<endl;
}

From an external text, not the inside. I only get the last paragraph but not all of it. I'm currently thinking of two possible options:

vector<string> text and push back every time "\n" are encountered. Fill it with the sentence. Or use the dynamic array where I count how many "\n" there are and fill it up.
closed account (DSLq5Di1)
#include <iterator>

1
2
3
4
ifstream text_input("test_text.txt");

string text(istreambuf_iterator<char>(text_input.rdbuf()),
            istreambuf_iterator<char>());
Topic archived. No new replies allowed.