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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:29:05+00:00 2026-06-12T16:29:05+00:00

Possible Duplicate: Testing stream.good() or !stream.eof() reads last line twice I’m working on an

  • 0

Possible Duplicate:
Testing stream.good() or !stream.eof() reads last line twice

I’m working on an assignment where I am required to read data from a file into a linked list. So far I am able to read the file into the linked list with no problem, however everytime i read from the file into the list, i get one extra entry:

here’s my struct:

struct Video { 
char video_name[1024];      // video name
int ranking;                // Number of viewer hits
char url[1024];             // video URL
YouTubeVideo *next;  // pointer to Video structure
}  *head = NULL;        // EMPTY linked list

here’s my read-in code:

void load()
{
ifstream rankFile ("Ranking.dbm");
struct Video *temp;
if (rankFile.is_open())
{
    string line;
    do {

        temp = (Video*)malloc(sizeof(Video)); //allocate space for node 
        temp->next = head;
        head = temp;
        rankFile.getline(temp->video_name,1024);
        rankFile >> temp->ranking;
        getline(rankFile, line); // need to skip 'ranking's
        // unread new-line
        rankFile.getline(temp->url,1024);


    }while (rankFile.good() );

    rankFile.close();
}

else cout << "Unable to open file"; 

return ;

}

it is reading from a file that looks like this:

spiderman
100
spiderman.com
lucy
30
lucy.com
disney
1200
disney.com

these all read in fine, however it appears that the do-while(rankFile.good()); loop goes through one more iteration giving me an empty node in the linked list. I have already tried using all of my getline() statements as conditions of the while loop and that didnt seem to change anything. Also using rankFile != eof gave me no luck.

Is there a way to be checking the next I/O to make sure its not \n or the end of file?

  • 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-12T16:29:06+00:00Added an answer on June 12, 2026 at 4:29 pm

    Change it to while(rankFile.good()) instead of using the do...while syntax. do...while doesn’t evaluate the terminating condition on your last iteration until after the contents of the loop have finished processing. A simple while will evaluate before processing the loop body.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Circle line collision detection I'm trying to do collision testing between a
Possible Duplicate: Auto-generation of .NET unit tests I am pretty new to Unit Testing
Possible Duplicate: How to use NSJSONSerialization I am testing to use the web service
Possible Duplicate: How do I set HTTP_REFERER when testing in Rails? I tried request.env[HTTP_REFERER]
Possible Duplicate: Testing if jQueryUI has loaded How to check if the jQuery UI
Possible Duplicate: Regex Testing Tools What sorts of tools do other programmers use when
Possible Duplicate: Using IoC for Unit Testing I think I do have a Problem
Possible Duplicate: How should I unit test threaded code? The classical unit testing is
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself
Possible Duplicate: Testing use of NSURLConnection with HTTP response error statuses This is bizarre;

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.