What I want to do is read a whole paragraph of text from a custom file. How would I do this without doing something silly like this?

while(someFile >> word1 >> word2 >> word3){
    //etc.
}

I need to be able to read 3 numbers for todays date, then read a whole paragraph, then another whole paragraph, and then repeat it.

Dani AI

Generated

Short answer: read the three ints with operator>>, consume the leftover newline, then use std::getline in a small helper that accumulates lines until you hit a blank line (that blank line is the paragraph separator). As hinted, getline is the right tool — but you must handle the leftover newline after the >> read and the possibility of multi-line paragraphs.

Example pattern (robust, handles multi-line paragraphs separated by an empty line):

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

static bool readParagraph(std::istream& in, std::string& out) {
    out.clear();
    std::string line;
    // skip leading blank lines
    while (std::getline(in, line)) {
        if (!line.empty() && line[line.size()-1] == '\r') line.erase(line.size()-1);
        if (line.empty()) continue;
        out = line;
        break;
    }
    if (out.empty()) return false; // EOF or no paragraph
    // gather following lines until a blank line
    while (std::getline(in, line)) {
        if (!line.empty() && line[line.size()-1] == '\r') line.erase(line.size()-1);
        if (line.empty()) break;
        out += "\n";
        out += line;
    }
    return true;
}

int main() {
    std::ifstream in("data.txt");
    if (!in) return 1;
    int y, m, d;
    while (in >> y >> m >> d) {
        std::string tmp;
        std::getline(in, tmp); // consume end-of-line after numbers
        std::string para1, para2;
        if (!readParagraph(in, para1)) break;
        if (!readParagraph(in, para2)) break;
        // process (y,m,d), para1, para2
    }
}

Notes and gotchas:

  • After reading numbers with >> you must remove the trailing newline (see the std::getline(in, tmp) above) or use std::ws before getline.
  • The helper trims a trailing '\r' to handle files with CRLF line endings.
  • If your paragraphs are single-line, you can call std::getline once per paragraph instead of the helper.
  • If blank lines are unreliable in the file, use an explicit delimiter (for example a line containing just ---END---) or prefix each paragraph with its length. Those formats make parsing unambiguous.

This approach should give you stable, readable parsing for the "3 numbers, paragraph, paragraph, repeat" layout that described.

Well you could setup something like this :

ifstream qwer ("p.txt");
Int number;
String paragraph;
qwer>>number;
getline(paragraph, qwer); // repeat for each line of paragraph with amend on or with a different  variable ll

The advantage of using amend is that you could just send it throug a loop instead of typeing it for each line.

Well you could setup something like this :

ifstream qwer ("p.txt");
Int number;
String paragraph;
qwer>>number;
getline(paragraph, qwer); // repeat for each line of paragraph with amend on or with a different  variable ll

The advantage of using amend is that you could just send it throug a loop instead of typeing it for each line.

How would I use that in the while loop though?

Well you could increment a number each loop (variable++) and when it reaches a point (while(variable!=5)) it breaks out of the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.