c++ - 读取文件中的多种数据类型并将其存储在链表中 C++
问题描述
如何读取文件中的多种数据类型并将其存储在链表中。我有下面显示的代码,但它通常读取没有链表的文件,但是当我使用链表时它不起作用。我认为我的代码有问题。编程语言:C++ 希望你能帮助我如何做到这一点。谢谢
struct Video {
int videoID;
string movieTitle;
string movieGenre;
string movieProduction;
int numberOfCopies;};
class videoList {
private:
struct videoNode {
Video video;
struct videoNode *next;
};
videoNode *head;
public:
videoList(); //constructor
//~videoList(); //deconstructor
void insertNewVideo (Video vid);
void rentVideo (Video vid);
void returnVideo (Video vid);
void showVideoDetails (Video vid);
void displayVideo ();
bool isVideoExist();
void exit ();
void generateVideoID();
void readVideoFile();};
void videoList::readVideoFile () {
videoNode *nodePtr;
ifstream file( "videoList.txt" ) ;
string line ;
string id, movieTitle, movieGenre, movieProduction, numberOfCopies;
if (!file) {
cout << endl << "\t\t\t\t There is file!" << endl;
}
else {
std::stringstream linestream(line);
cout << endl << "\t\t\t\t Movie List " << endl;
nodePtr = head;
while (nodePtr)
while (getline(file, line)){
{
std::getline(linestream, id, ',');
std::getline(linestream, nodePtr->video.movieTitle, ',');
std::getline(linestream, nodePtr->video.movieGenre, ',');
std::getline(linestream, nodePtr->video.movieProduction, ',');
linestream >> nodePtr->video.numberOfCopies;
if(linestream) // true if there were no errors reading the stream
{
std::cout << id << '\n';
std::cout << nodePtr->video.movieTitle << '\n';
std::cout << nodePtr->video.movieGenre << '\n';
std::cout << nodePtr->video.movieProduction << '\n';
std::cout << nodePtr->video.movieTitle << '\n';
}
}}}}
解决方案
推荐阅读
- pandas - 如何在熊猫数据框列上应用条件?
- flutter - Flutter:将整个应用程序上的默认 textDirection 更改为 RTL
- ruby-on-rails - Has_many 通过关系“user_roles”不存在
- python-3.x - 在 Python 中实现抽象类的最佳方法
- java - Postgres 查询 - 使用文本类型更新 json 列中的字段
- elasticsearch - 从 Spring Data Elasticsearch 4.0.0.M3 迁移到 M4
- python - 如何在python中为类和子类添加单独的id
- docker - AKS Ingress 导航到 URL 路径返回 404,将 url 更改为 index.html 哪个是正确的路由?
- python - ISO 格式的日期时间,毫秒到 000
- directus - 在directus中从模块添加监听器