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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:05:36+00:00 2026-06-14T14:05:36+00:00

I have a weird issue with regards to getline while reading from file. Below

  • 0

I have a weird issue with regards to getline while reading from file.

Below is the code.

#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

struct call {
    string l;
            string k;
};

int main () {
    ifstream read;
    string fn = "in.txt";
    string temp;
    call mc;
    vector<call> vcall;

    read.open (fn.c_str ());

    while (getline (read, temp)) {

        temp.erase (temp.end () - 1);

        if (temp.substr (0,1).compare ("L") == 0) {
            temp = temp.substr (7);
            stringstream ss (temp);
            while (ss) {
                getline (ss, mc.l, ':');
                getline (ss, mc.k, ':');
                vcall.push_back (mc);
            }
        }
    }
    for (int i = 0; i < vcall.size (); i++) {
        cout << vcall [i].i << vcall [i].k << endl;
    }

}

The text file in.txt contains a line.

Los:0:A:1:B:2:C:3:D:4:E:

The output from the program gives me.

0A
1B
2C
3D
4E
4E

Not sure why is it so. It should not have the last (another) 4 in the output.

I’ve tried re-creating the textfile, re-writing the codes in another file and compile.
Tried in Ubuntu (g++), and Windows (mingw) and still having the same issue.

Update:

The reason why I didn’t use while (getline (ss, mc.l, ‘:’)) is because I need multiple part. I made a slight changes to the code above, with structure having 2 string to store.
Also made changes to my text file to reflect the changes.

So how can I do it? Thanks.

  • 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-14T14:05:37+00:00Added an answer on June 14, 2026 at 2:05 pm

    Change this:

     while (ss) {
         getline (ss, mc.l, ':');
         vcall.push_back (mc);
     }
    

    To this:

    while (getline(ss, mc.l, ':')) {
        vcall.push_back(mc);
    }
    

    The problem is that you don’t check if the read fails until you’ve already added the item to the vector. So when the last read fails, the item, which remains unchanged from the previous call to getline, gets pushed back onto the vector again. The modified version bails out as soon as the read fails. You used this idiom in your primary for loop, the one that reads lines from the file. So I wonder why you didn’t use it with the stringstream.

    In response to the update:

    while (getline(ss, mc.l, ':') && getline(ss, mc.k, ':')) {
        vcall.push_back(mc);
    }
    

    If you find the condition in your while statement getting too unwieldy, you can always do something like this:

    while (true) {
        getline(ss, mc.l, ':');
        getline(ss, mc.k, ':');
        if (!ss)
            break;
        vcall.push_back(mc);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue reading a .csv file using c#. Here are the
I have been facing a weird issue in a piece of code. void app_ErrDesc(char
Have a weird issue. I am upgrading from 5.6.2 and following the documented suggested
I have a weird issue that is probably simple here is the code js
I have a weird issue with this code Moonbeam.Input.KeyboardState = function Moonbeam_Input_KeyboardState() { this._keys
I have a weird issue that comes up while releasing an object. In the
I have a weird issue with firebug with my current javascript code I have
We have a semi-weird issue for our site. Consistently, from 1 anonymous* user the
I have a weird issue with creating an Bitmap in C++. I'm using the
I have a weird issue when connecting to Oracle with Groovy. I have created

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.