C++ 我无法将数据从文本文件传递到队列

C++ 我无法将数据从文本文件传递到队列,c++,data-structures,queue,file-handling,C++,Data Structures,Queue,File Handling,我正在创建一个程序来存储文本文件中的数据,并将其排入队列。我已经为它创建了一个函数,但是它没有将数据存储到我的队列中 以下是我从文本文件获取数据并将其插入队列的函数: void TextToQueue() { Queue<Information> link; //Connecting my Queue to my structure called Information Information TextInfo; //Information is a structur

我正在创建一个程序来存储文本文件中的数据,并将其排入队列。我已经为它创建了一个函数,但是它没有将数据存储到我的队列中

以下是我从文本文件获取数据并将其插入队列的函数:

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.";
    }
}
void TextToQueue(){
队列链接;//将队列连接到名为信息的结构
信息TextInfo;//信息是包含字符串名称、地址和ID的结构
Temp_File t;//Temp_File是包含字符串数据和类型的结构。
流文件;
int i=0;
字符分隔符=':';
字符分隔符r1='\n';
打开(“Sample.txt”,ios::in);
if(FILE.is_open()){
while(getline(FILE,t.type,delimiter)和&getline(FILE,t.data,delimiter1)){
如果(i==0){
TextInfo.Name=t.data;//我的t.data中的数据将存储在TextInfo.Name中
i++;
}
else如果(i==1){
TextInfo.Address=t.data;//我的t.data中的数据将存储在TextInfo.Address中
i++;
}
else如果(i==2){
TextInfo.ID=t.data;//我的t.data中的数据将存储在TextInfo.ID中
link.push(TextInfo);//将数据推送到或排队到我的队列中
i=0;//每当文件中有大量数据时,i的值将再次变为零,以重复整个过程。
}
}
FILE.close();
}
否则{
std::cout值=数据;
tmp->next=NULL;
如果(!前){
后部=tmp;
前端=tmp;
}
否则{
后->下一步=tmp;
后部=tmp;
}
}

您的问题是循环和if语句的本地作用域。每次为
信息
分配属性时,都会调用构造函数。解决方法是使用
自动TextInfo=new Information
构造函数,推送它,然后将
TextInfo
重新分配给一个新对象。只需考虑销毁正在删除创建的对象。或者您可以将
TextInfo
推送到队列中,然后只修改属性并在最后一个子句中推送一个新的空属性。

link
是一个不会返回的局部变量。您不能从函数外部访问它。解决问题的一个方法是通过引用
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;
    }
}
#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";
}
输出:

Data: 
"Address"
"ID"
"Name"

link.push(C_Info_LList)
…何时何地定义和初始化
C\u Info\u LList
变量?为什么不在函数中按一下初始化的
TextInfo
?哦,对不起。忘记了完全编辑我的变量。请稍候。我在发布此问题时更改了变量名称。这是您应该尝试创建一个正确的变量的原因之一在发布之前,请确保它复制了问题。另一件事,您在
TextToQueue
函数中定义了局部变量
link
。该变量的生命周期将在函数结束时结束,您推送到该变量的所有数据都将丢失。您应该返回队列还是修改member或名为
link
的全局变量?我们不需要完整的类。相反,请考虑何时何地定义
link
变量。正如我所说,它是函数内部的局部变量。一旦函数返回变量
link
就会消失,您添加到队列中的所有节点也会消失。也许你需要退后一步,更新关于可变范围和生命周期的知识?谢谢!它成功了!但是,我有一个问题。这可能是一个愚蠢的问题,但它将在将来帮助我更好地理解它。为什么我需要通过引用来传递它?通过引用传递与非引用传递有什么区别?@Richard通过值传递会创建一个副本,不会解决您的问题。您仍然会有两个不同的队列,一个在函数内有数据,另一个在函数外没有数据。
Data: 
"Address"
"ID"
"Name"