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++ :/
This is incorrect use of
std::istream::getline():and does not make any sense as there are two input streams involved:
cinandrankFile. The correct invocation (but not the most preferable) is:Suggest:
std::stringinstead ofchar[]and usestd::getline(in, std::string&).operator>>to read theintas you cannot usestd::getline()for this.malloc()in C++ usenewanddelete.std::vector<Video>for example.For example: