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

  • SEARCH
  • Home
  • 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 7908713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:04:33+00:00 2026-06-03T12:04:33+00:00

the problem that im having is this that i am able to create a

  • 0

the problem that im having is this that i am able to create a text file, write to a text once. i want to be able to add more lines to it when necessary without creating a new file. my next problem is that i cant seem to get the output that im looking for. eg. text file contain

start1
fname|lname|ssn
end1

my aim is to only get data within start1 and end1 and return fname lname and ssn without the delimiter |. here is my code

int main()
{
    fstream filestr;
    string line;

    filestr.open ("file.txt", fstream::in | fstream::out | fstream::app);
    if(!filestr.is_open())
    {
        cout << "Input file connection failed.\n";
        exit(1); 
    }
    else{
        filestr<<"Start2\n";
        filestr<< "middle|middle"<<endl;
        filestr<<"end2"<<endl;
        if(filestr.good()){
            while(getline(filestr, line) && line !="end1"){
                if(line !="Start1"){
                    //below this point the output goes screwy
                    while(getline(filestr, line,'|')){
                        cout<<"\n"<<line;
                    }
                }
            }
        }
        filestr.close();
    }
  • 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-03T12:04:35+00:00Added an answer on June 3, 2026 at 12:04 pm

    Nearly:

    When you open a file for appending the read position is at the end.
    So before you start reading you need to seek back to the beginning (or close and re-open).

            filestr.seekg(0);
    

    The second probelem is that you nested while loop does not check for end:

                    while(getline(filestr, line,'|')){
                        cout<<"\n"<<line;
    

    This breaks up the line. But it does not stop at the end of the line. it keeps going until it reaches the end of the file.

    What you should do is take the current line and treat it as its own stream:

                if(line !="Start1")
                {   
                    std::stringstream   linestream(line);
                  // ^^^^^^^^^^^^^^^^^^  Add this line
    
                    while(getline(linestream, line,'|'))
                    {         //  ^^^^^^^^^^ use it as the stream
                        cout<<"\n"<<line;
                    }   
                }
    

    PS: In the file.txt

    start1
    ^^^^^ Note not Start1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create this effect: http://www.thecssninja.com/demo/css_tree/ the problem I'm having is that the
I'm having a problem with this query that takes several seconds to complete. I
I am having a problem like this one: http://svn.haxx.se/tsvnusers/archive-2008-07/1051.shtml Except that the app is
I'm having a problem. I have a UIView, that looks like this : In
I am having some problems figuring out this issue. I have a server that
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
I am having problem that when i am trying to submit the form by
I've been thinking/searching for an equivalent of the problem that I am having below,
HI Guys, Here I am having problem that How can I post an image
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when

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.