我嘗試寫一個程序,從文本文件讀入一個鏈表從void *的轉換無效爲char * C++從文件讀取鏈表
這裏是表結構。
#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
這裏是讀取到的代碼:
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 ;
}
它是從一個文本文件Ranking.dbm
它看起來像這樣寫着:
bagheera
20
bagheera.com
sushi
60
sushi.com
wicket
99
wicket.com
teek
100
teek.com
但是我收到一個錯誤說: Invalid conversion from void* to char*
對於我的cin.getline()
聲明中的所有3個語句,它是從文件讀取的。我需要能夠從我的文件(Ranking.dbm
)中逐行讀取,並將每組3行存儲到temp->video_name
,temp->ranking
和temp->url
,然後創建新節點並保存接下來的3行...等等等等直到讀入文件中的所有內容。
我該怎麼做?我是以完全錯誤的方式去解決這個問題還是僅僅是一個語法錯誤?我仍然得到C的掛++:/
這是C++?看起來像一個奇怪的C混搭。 – chris
對不起,我試圖轉移一箇舊的C程序工作在C++和即時通訊仍然得到我應該和不應該使用的竅門... – accraze
字符數組更好地替換爲'std: :string'。 'cin.getline'然後將被替換爲'std :: getline'。節點最好是智能指針。 C++甚至有一個鏈表類 - 「std :: list」。 – chris