How would I do this?

How would I read an entire paragraph (from a text file) into a single character array or string, doesn't matter which? How would I do that?
If the text file delimits paragraphs the usual way (a new line character), then just use the global function std::getline(). Even if they were delimited with some other character, I believe this function would still be able to do the job.
But what if the text file contains numerous newline characters? ARGHHHHH lawl
This should help a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
  vector<string> v;
  ifstream in("Fillvector.cpp");
  string line;
  while(getline(in, line))
    v.push_back(line); // Add the line to the end
  // Add line numbers:
  for(int i = 0; i < v.size(); i++)
    cout << i << ": " << v[i] << endl;
} ///:~ 


Then after that maybe concatenate them all into one string? I'm not really sure, sorry I can't be more help.
You can write a specific format?


I think if you can, I may be able to give you help
Simply use the getline() function with the delimiter as char(26).
It should read the text into a string at once
If there are multiple paragraphs, and between paragraphs you have multiple newline delimiters, I believe getline() will produce an empty string. If this happens, just ignore it and move on to the next line.
But what if the text file contains numerous newline characters? ARGHHHHH lawl

What would there be a point in a getline() function if it couldn't do this? ;)
Last edited on
Topic archived. No new replies allowed.