c++ - 从双链表中的文件读取数据
问题描述
我创建了一个简单的双向链表来存储有关植物的数据。我需要从文件 c++ 中写入和读取数据。写入文件没有问题。但我不知道如何正确地从文件中读取数据(。也许有一些功能。
早些时候,当我将数据从文件写入数组时,我使用循环按索引对元素进行排序。但是现在当我需要将元素写入双链表时 - 我真的不明白该怎么做。
这是我的代码:
#include <iostream>
#include <cstring>
#include <list>
#include <fstream>
#include <locale>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <list>
using namespace std;
typedef struct
{
char plant[100];
char family[100];
char species[100];
}BOOK;
typedef struct tag_obj
{
BOOK b;
struct tag_obj* prev, * next;
}OBJ;
OBJ* head = NULL, * tail = NULL;
void add_obj(OBJ* obj, BOOK book, char sim[50]) {
OBJ* c = head;
while (c != NULL) {
if (strcmp(c->b.plant, sim) == 0){
cout<< " element already existst(("<< endl;
return ;
}
c = c->next;
}
OBJ* ptr = new OBJ;
ptr->b = book;
ptr->prev = obj;
ptr->next = (obj == NULL) ? NULL : obj->next;
if (obj != NULL) {
obj->next = ptr;
if (obj->next != NULL) obj->next->prev = ptr;
}
if (ptr->prev == NULL) head = ptr;
if (ptr->next == NULL) tail = ptr;
}
void del_obj(OBJ* obj) {
char x[50];
cout << "enter the name of the plant whose data you want to delete: ";
cin >> x;
OBJ* tmp;
OBJ* h;
if (head == NULL)
{
cout << "List empty,nothing to delete" << endl;
return;
}
if (strcmp(head->b.plant, x) == 0)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = head;
while (h->next->next != NULL)
{
if (strcmp(h->next->b.plant, x) == 0)
{
tmp = h->next;
h->next = tmp->next;
tmp->next->prev = h;
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = h->next;
}
if (strcmp(h->next->b.plant, x) == 0)
{
tmp = h->next;
free(tmp);
h->next = NULL;
cout << "Element Deleted" << endl;
return;
}
cout << "Element " << x << " not found" << endl;
}
void show() {
OBJ* c = head;
int d = 1;
while (c != NULL) {
cout << d << ")" << " " << c->b.plant << " " << c->b.family << " " << c->b.species << endl;
c = c->next;
d++;
}
}
int main() {
//setlocale(LC_ALL, "ukr");
ofstream fout;
fout.open("plant account.txt", ios::app);
ifstream fin;
fin.open("plant account.txt");
if (!fout.is_open())
{
cout << "Помилка вiдкриття файлу";
return 1;
}
char sim[50];
BOOK book = { " PLANT "," FAMILY " };
//add_obj(tail, book);
int choice;
cout << "**plant account**" << endl;
cout << "1(add an element)" << endl;
cout << "2(delete)" << endl;
cout << "3(show)" << endl;
cout << "4(exit)" << endl << endl;
start:
cout << " Choose action : ";
cin >> choice;
switch (choice)
{
case 1:
fout << endl;
cout << "Plant name: ";
cin >> book.plant;
strcpy_s(sim, book.plant);
fout << book.plant << " ";
cout << "Plant family: ";
cin >> book.family;
fout << book.family << " ";
cout << "Plant species: ";
cin >> book.species;
fout << book.species << " ";
add_obj(tail, book,sim);
cout << endl;
goto start;
case 2:
del_obj(head);
cout << endl;
goto start;
case 3:
show();
cout << endl;
goto start;
case 4:
cout << " " << endl;
exit(222);
default:
break;
}
fout.close();
fin.close();
return 0;
}
解决方案
推荐阅读
- python - 如何在 Python 中修复“re.error:位置未终止的字符集”?
- angular - 在同一服务中模拟一个函数以进行单元测试
- python - 取数组中每 n 个元素的平均值并将 MATLAB 代码转换为 Python
- angular - 在 httmclient 方法 Angular 中读取 json 文件
- c# - 在 LINQ 中连接多个表
- python - 如何在 Kivy 中为倒数计时器创建圈数功能
- c++ - 为什么 std::strcpy 仍然适用于 char copyTo[0] 这样的目的地?
- azure-devops - Azure 构建:您是否缺少程序集参考?
- c++ - 如何检查类型 T 是否在参数包 Ts 中...?
- java - 如何将意图发送到游戏视图