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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:21:06+00:00 2026-06-12T09:21:06+00:00

im trying to write a program that reads from a text file into a

  • 0

im trying to write a program that reads from a text file into a linked list

here is the list structure.

#include <iostream> 
#include <string>
#include <fstream>
using namespace std;


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

here is the read-in code:

void load()
{
struct Video *temp;
temp = (Video*)malloc(sizeof(Video)); //allocate space for node 
temp = head;
ifstream rankFile ("Ranking.dbm");
if (rankFile.is_open())
{
    while ( rankFile.good() )
    {
        cin.getline(rankFile, temp->video_name, "\n");
        cin.getline(rankFile, temp->ranking, "\n");
        cin.getline(rankFile, temp->url, "\n");
        temp = temp->next; 

    }
    myfile.close();
}

else cout << "Unable to open file"; 

return ;

}

it is reading from a text file Ranking.dbm which looks like this:

bagheera
20
bagheera.com
sushi
60
sushi.com
wicket
99
wicket.com
teek
100
teek.com

however i am getting an error saying: Invalid conversion from void* to char* on all 3 of my cin.getline() statements while it is reading from the file. I need to be able to read in line by line from my file (Ranking.dbm) and store each set of 3 lines to temp->video_name , temp->ranking and temp->url and then create a new nodes and save the next 3 lines… so on and so forth till ive read in everything from the file.

how can i do this? am i going about this in a completely wrong manner or is this just a syntax error? i’m still getting the hang of C++ :/

  • 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-12T09:21:07+00:00Added an answer on June 12, 2026 at 9:21 am

    This is incorrect use of std::istream::getline():

    cin.getline(rankFile, temp->video_name, "\n");
    

    and does not make any sense as there are two input streams involved: cin and rankFile. The correct invocation (but not the most preferable) is:

    rankFile.getline(temp->video_name, 1023);
    

    Suggest:

    • using std::string instead of char[] and use std::getline(in, std::string&).
    • use operator>> to read the int as you cannot use std::getline() for this.
    • check result of every read operation.
    • don’t use malloc() in C++ use new and delete.
    • don’t dynamically allocate if you don’t have to.
    • use one of the STL containers for holding a list rather than implementing that yourself, std::vector<Video> for example.

    For example:

    struct Video { 
        std::string video_name;
        int ranking;
        std::string url;
    };
    
    std::vector<Video> load()
    {
        std::vector<Video> result;
        std::ifstream rankFile("Ranking.dbm");
        if (rankFile.is_open())
        {
            Video temp;
            std::string line;
            while (std::getline(rankFile, temp.video_name) &&
                   rankFile >> temp.ranking &&
                   std::getline(rankFile, line) && // need to skip 'ranking's
                                                   // unread new-line
                   std::getline(rankFile, temp.url))
            {
                result.push_back(temp);
            }
        }
        else
        {
            std::cerr << "Unable to open file"; 
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a little C# program that reads from a text file
I'm trying to write a program that reads text from external file (string string
I am trying to write a Java program that reads an input file consisting
So, I'm trying to write a c program that reads input piped into the
I am trying to write a program that sends basic text files from one
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a java program that reads data from an image and
I am trying to write a program for class that reads a list of
I am trying to write a program that will create a text file and
I am trying to write a list to a text file that is included

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.