-3
嗨,大家好我即将尝试将旧代码从C转移到C++进行分配....我必须手动实现链表,否则我不能使用STL容器, ð已经与这个工作....C++从文件读入链接列表给EXC_BAD_ACCESS
这里是我的链接列表:
#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 myfile ("Ranking.dbm");
if (myfile.is_open())
{
string line;
while (myfile.good())
{
myfile.getline(temp->video_name,1024);
myfile >> temp->ranking;
getline(myfile, line); // need to skip 'ranking's
// unread new-line
myfile.getline(temp->url,1024);
temp = temp->next;
}
head = NULL;
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
基本上每组3行应该加载到Video
结构,这将是在所述链接列表的新节点。
由于我无法控制的电流,我在此项目中使用XCode。我的问题是为什么我得到这个错误。我以为EXC_BAD_ACCESS主要是一个Objective-C错误...?
你有什么工作在C? – Beta
为什么不抽象一个链表的概念,所以你不读东西*和*直接操纵数据结构?或者,更好的是,使用'std :: list <>',它实现一个链表。 – cdhowie
你只分配一个视频结构(并且永远不会分配给它的下一个),但是你的代码似乎假设整个链表已经被分配了...... – Cameron