如何解决我无法将数据从我的文本文件传递到我的队列
我正在创建一个程序,该程序将存储我的文本文件中的数据并将其排入我的队列。我已经为它创建了一个函数,但是它没有将数据存储到我的队列中。
void TextToQueue() {
Queue<information> link; //Connecting my Queue to my structure called information
information TextInfo; //information is a structure containing string Name,Address,and ID
Temp_File t; //Temp_File is a structure containing string data and type.
fstream FILE;
int i = 0;
char delimiter = ':';
char delimiter1 = '\n';
FILE.open("Sample.txt",ios::in);
if (FILE.is_open()) {
while (getline(FILE,t.type,delimiter) && getline(FILE,t.data,delimiter1)) {
if (i == 0) {
TextInfo.Name = t.data; //The data inside my t.data will be stored in TextInfo.Name
i++;
}
else if (i == 1) {
TextInfo.Address = t.data; //The data inside my t.data will be stored in TextInfo.Address
i++;
}
else if (i == 2) {
TextInfo.ID = t.data; //The data inside my t.data will be stored in TextInfo.ID
link.push(TextInfo); //Pushing or Enqueuing the data to my queue
i = 0; //the value of i will become zero again to repeat the whole process whenever there are a lot of data inside the file.
}
}
FILE.close();
}
else {
std::cout << "File is not open.";
}
}
这是我将数据存储到队列的推送函数。每当我向文本文件插入新数据时,推送功能都会起作用,但是我认为问题出在 TextToQueue 功能中:
template <class T>
void Queue<T>::push(T data) {
Queue* tmp = new Queue;
tmp->value = data;
tmp->next = NULL;
if (!front) {
rear = tmp;
front = tmp;
}
else {
rear->next = tmp;
rear = tmp;
}
}
解决方法
您的问题是循环和 if 语句的局部范围。每次将属性分配给 Information
时,都会调用构造函数。解决方案是使用 auto TextInfo = new Information
构造函数,推送它,然后将 TextInfo
重新分配给一个新对象。只需要考虑销毁创建的对象。或者您可以将 TextInfo
推入队列,然后修改属性并在最后一个子句中推入一个新的空白。
link
是一个不返回的局部变量。您无法从函数外部访问它。您的问题的一种解决方案是通过引用 TextToQueue
传递一个队列,将其填充到函数内部并在函数外部读取:
#include <fstream>
#include <iostream>
#include <string>
template <class T>
struct Queue {
T value;
Queue* front;
Queue* rear;
Queue* next;
void push(T data);
};
template <class T>
void Queue<T>::push(T data) {
Queue* tmp = new Queue;
tmp->value = data;
tmp->next = NULL;
if (!front) {
rear = tmp;
front = tmp;
}
else {
rear->next = tmp;
rear = tmp;
}
}
struct Information {
std::string ID;
std::string Name;
std::string Address;
};
struct Temp_File {
std::string type;
std::string data;
};
void TextToQueue(Queue<Information> &link) {
Information TextInfo; //Information is a structure containing string Name,Address,and ID
Temp_File t; //Temp_File is a structure containing string data and type.
std::fstream FILE("Sample.txt",std::ios::in);
int i = 0;
char delimiter = ':';
char delimiter1 = '\n';
if (FILE.is_open()) {
while (getline(FILE,t.type,delimiter) && getline(FILE,t.data,delimiter1)) {
if (i == 0) {
TextInfo.Name = t.data; //The data inside my t.data will be stored in TextInfo.Name
i++;
}
else if (i == 1) {
TextInfo.Address = t.data; //The data inside my t.data will be stored in TextInfo.Address
i++;
}
else if (i == 2) {
TextInfo.ID = t.data; //The data inside my t.data will be stored in TextInfo.ID
link.push(TextInfo); //Pushing or Enqueuing the data to my queue
std::cout << "push\n";
i = 0; //the value of i will become zero again to repeat the whole process whenever there are a lot of data inside the file.
}
}
FILE.close();
}
else {
std::cout << "File is not open.";
}
}
int main() {
Queue<Information> link; //Connecting my Queue to my structure called Information
TextToQueue(link);
std::cout << "Data: \n";
std::cout << '"' << link.front->value.ID << "\"\n";
std::cout << '"' << link.front->value.Name << "\"\n";
std::cout << '"' << link.front->value.Address << "\"\n";
}
文件:
id:ID
name:Name
address:Address
输出:
Data:
"Address"
"ID"
"Name"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。