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 7811791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:17:45+00:00 2026-06-02T04:17:45+00:00

I am supposed to write a very little program analogical to Unix tail function,

  • 0

I am supposed to write a very little program analogical to Unix tail function, I’m using std::deque to store the lines I read by std::getline from supplied file. Pushing in the front, popping from the back.

My problem is, that when I try to print more lines then there are in file, it outputs 1 extra blank line at the begining of output. Here is the source code, TParams is struct with int lncount where number of lines requested is stored, and some other at the moment unimportant stuff…

using namespace std;

deque<string> dq;
int counter = 0;

for(string line; ! (*infile).eof(); getline(*infile, line)){
    dq.push_front(line);

    // not needed lines dropped immediately
    if(counter++ >= TParams.lncount)
        dq.pop_back();
}

int iter = (TParams.lncount > dq.size()) ?
           (dq.size() - 1) : (TParams.lncount - 1);

assert(iter < dq.size());

for(iter; iter >= 0; iter--)
    cout << dq[iter] << endl;

There is some more code regarding -n +num parameter, but it is inside conditions and does not affect this case.

I found out, that there actually is zero length string stored in dq.back() but I have absolutely no idea where it comes from, as it should be the one read from beginning of file, but there is normal line of text.

  • 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-02T04:17:49+00:00Added an answer on June 2, 2026 at 4:17 am

    Your read loop is wrong. First, the value (*infile).eof() isn’t
    really specified; if you have an empty file, it could still return
    false. And secondly, you’re not reading a line before entering the
    loop to begin with. The classic way to write a loop like this would be:

    std::string line;
    while ( std::getline( *infile, line ) ) {
        //  ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to write program to convert infix notation to postfix notation using
I'm trying to write a simple program using WinSnmp in C++. There is very
I am supposed to write a program in JavaScript to find all the anagrams
I am supposed to write a program that gets some PNG images from the
This code is supposed to read postfix problems from a file and and write
I'm trying to write a program that is just a little beyond my abilities.The
Current assignment needs me to write a program to read a file with instructions
My application is supposed to read and write to a Serial Port. The data
Given the following code (it's supposed to write helloworld in a helloworld file, and
Suppose I let the user to write a condition using Javascript, the user can

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.