Click here to Skip to main content
15,909,566 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a file with the following data:

Line 1 - Name
Line 2 - Description - Two or more paragraphs
Line 3 - Number

How do I read the file and put each line into a variable (Note - Line two has two or more paragraphs.).

What I have tried:

I tried using getline(file, line), but the two paragraphs messes it up. Thank You...
Posted
Comments
CPallini 16-May-24 9:24am    
Is 'Line 2' actually a line?
Could you please provide an example?
k4ghg 17-May-24 9:08am    
Its multiple lines (i.e., two paragraphs).
This is an example of line two. There are several sentences with many words. Writing junk as an example. AS you might tell.

Line 2 continues with paragraph number 2. I add add junk to fill in the data of the eample. Thank You...
k4ghg 16-May-24 10:00am    
Line 2, Is more than a single line. It could be two or three paragraphs.
Pete O'Hanlon 16-May-24 10:01am    
So, what you are really asking for is:
Header: string
Body: Multiple lines.
Footer: number

Is that correct?
k4ghg 17-May-24 9:05am    
Yes...

1 solution

If my assumption is correct about what you are asking for, the way that you want to accomplish this task is to break it down into a simple process. You know that the first line is the header, so you read that line in and assign it as the header. Then you have multiple lines; the last one of which is the footer. Basically, read the file in line by line; when you reach the end of file marker, you know that this is the footer. Everything in between is the paragraph text. Note that there's an implicit assumption here that the last line is the footer, and not a blank line. Something like this might fit your purposes:
C++
void getTextFromFile(std::string filename) {
    std::ifstream file(filename);

    // put error handling here to cater for the file not opening

    std::string header;
    std::getline(file, header);
    std::cout << "Header: " << header << std::endl;

    std::string line;
    while (std::getline(file, line)) {
        if (file.eof()) {
            // Last line, treat it as the footer
            std::cout << "Footer: " << line << std::endl;
            break;
        }
        std::cout << line << std::endl;
    }

    file.close();
}
 
Share this answer
 
v3
Comments
k4ghg 16-May-24 10:47am    
Hi Pete - Thank You... I tried your suggestion (Code listed below) and am getting some errors and which I don't know why. Again, thank you:

error: a function-definition is not allowed here before '{' token
error: expected '}' at end of input

#include <fstream>
#include <iostream>

using namespace std;

int main (){

void getTextFromFile(std::string filename) {
std::ifstream file("Out.txt");

ifstream reader();
if (!reader)
{
cout << "Error opening input file" <
k4ghg 16-May-24 10:48am    
return - 1;
}
// put error handling here to cater for the file not opening

std::string header;
std::getline(file, header);
std::cout << "Header: " << header << std::endl;

std::string line;
while (std::getline(file, line)) {
if (file.eof()) {
// Last line, treat it as the footer
std::cout << "Footer: " << line << std::endl;
break;
}
std::cout << line << std::endl;
}

file.close();
}
}
Pete O'Hanlon 16-May-24 11:12am    
You put a function call inside your main function. Move the function before main, and then call it from inside your main function.
k5054 16-May-24 11:33am    
I think you meant a function definition, rather than a call, just to clarify for the OP
jeron1 16-May-24 11:14am    
Have you ever used a function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900