正在读取文件;。txt";并将每行的数据分配给循环列表C+中的新节点+; 我正在使用C++进行一个项目,我被要求读取一个TXT文件并将数据分配给一个节点。 文本文件为:

正在读取文件;。txt";并将每行的数据分配给循环列表C+中的新节点+; 我正在使用C++进行一个项目,我被要求读取一个TXT文件并将数据分配给一个节点。 文本文件为:,c++,stream,codeblocks,fstream,ifstream,C++,Stream,Codeblocks,Fstream,Ifstream,亚尼,19,1993,12345 奥斯卡,20,1994,56789 我有结构列表: struct List{ string name; int age; int birth; int id; List *next; List *prev; } 我的问题是,如何将文本文件中的数据分配给节点(在本例中为两个节点),但如果有更多的行,则必须创建更多的新节点 我正在使用的部分代码: #include <fstream> struct Li

亚尼,19,1993,12345

奥斯卡,20,1994,56789

我有结构列表:

struct List{
    string name;
    int age;
    int birth;
    int id;
    List *next;
    List *prev;
}
我的问题是,如何将文本文件中的数据分配给节点(在本例中为两个节点),但如果有更多的行,则必须创建更多的新节点

我正在使用的部分代码:

#include <fstream>

struct List{
...
}

//Here I create the first Node and:
aux=head;
ifstream file ("file.txt");
file >> aux->name >> aux->age >> aux->birth >> aux-> id;
#包括
结构列表{
...
}
//在这里,我创建了第一个节点,并:
aux=头部;
ifstream文件(“file.txt”);
文件>>辅助->姓名>>辅助->年龄>>辅助->出生>>辅助->id;
如果文本文件中没有逗号,则可以正常工作,但逗号会到处出错

此外,总结如下:) 如果文本文件包含:

丹尼·沃森,231980,58953

节点的名称必须是Danny Watson,而不仅仅是Danny:)

我希望你能帮助我!我会非常感激的


(对不起,我的英语不是很好:()

问题是输入操作符
>
读取一个空格分隔的“单词”。这意味着像
“Danny Watson”
这样的输入将被读取为两个单独的单词

相反,我建议您使用将整行读入a,然后再使用an获得逗号分隔的值。如果需要,请记住去掉前导(以及可能的尾随)空格

大概是这样的:

std::vector<List> myList;

std::string line;
while (std::getline(file, line))
{
    std::istringstream iss(line);

    List myEntry;

    // Read a comma-separated entry from the temporary input stream
    std::getline(iss, myEntry.name, ',');

    std::string temp;

    std::getline(iss, temp, ',');
    myEntry.age = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.birth = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.id = std::stoi(temp);

    myList.push_back(myEntry);
}
std::vector myList;
std::字符串行;
while(std::getline(文件,行))
{
标准::istringstream iss(线);
列出我的中心;
//从临时输入流中读取逗号分隔的条目
std::getline(iss,myEntry.name,,);
std::字符串温度;
std::getline(iss,temp,',');
myEntry.age=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.birth=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.id=std::stoi(temp);
myList.push_back(myEntry);
}

问题在于,输入操作符
>
读取一个空格分隔的“单词”。这意味着像
“Danny Watson”
这样的输入将被读取为两个单独的单词

相反,我建议您使用将整行读入a,然后再使用an获得逗号分隔的值。如果需要,请记住去掉前导(以及可能的尾随)空格

大概是这样的:

std::vector<List> myList;

std::string line;
while (std::getline(file, line))
{
    std::istringstream iss(line);

    List myEntry;

    // Read a comma-separated entry from the temporary input stream
    std::getline(iss, myEntry.name, ',');

    std::string temp;

    std::getline(iss, temp, ',');
    myEntry.age = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.birth = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.id = std::stoi(temp);

    myList.push_back(myEntry);
}
std::vector myList;
std::字符串行;
while(std::getline(文件,行))
{
标准::istringstream iss(线);
列出我的中心;
//从临时输入流中读取逗号分隔的条目
std::getline(iss,myEntry.name,,);
std::字符串温度;
std::getline(iss,temp,',');
myEntry.age=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.birth=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.id=std::stoi(temp);
myList.push_back(myEntry);
}

问题在于,输入操作符
>
读取一个空格分隔的“单词”。这意味着像
“Danny Watson”
这样的输入将被读取为两个单独的单词

相反,我建议您使用将整行读入a,然后再使用an获得逗号分隔的值。如果需要,请记住去掉前导(以及可能的尾随)空格

大概是这样的:

std::vector<List> myList;

std::string line;
while (std::getline(file, line))
{
    std::istringstream iss(line);

    List myEntry;

    // Read a comma-separated entry from the temporary input stream
    std::getline(iss, myEntry.name, ',');

    std::string temp;

    std::getline(iss, temp, ',');
    myEntry.age = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.birth = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.id = std::stoi(temp);

    myList.push_back(myEntry);
}
std::vector myList;
std::字符串行;
while(std::getline(文件,行))
{
标准::istringstream iss(线);
列出我的中心;
//从临时输入流中读取逗号分隔的条目
std::getline(iss,myEntry.name,,);
std::字符串温度;
std::getline(iss,temp,',');
myEntry.age=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.birth=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.id=std::stoi(temp);
myList.push_back(myEntry);
}

问题在于,输入操作符
>
读取一个空格分隔的“单词”。这意味着像
“Danny Watson”
这样的输入将被读取为两个单独的单词

相反,我建议您使用将整行读入a,然后再使用an获得逗号分隔的值。如果需要,请记住去掉前导(以及可能的尾随)空格

大概是这样的:

std::vector<List> myList;

std::string line;
while (std::getline(file, line))
{
    std::istringstream iss(line);

    List myEntry;

    // Read a comma-separated entry from the temporary input stream
    std::getline(iss, myEntry.name, ',');

    std::string temp;

    std::getline(iss, temp, ',');
    myEntry.age = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.birth = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.id = std::stoi(temp);

    myList.push_back(myEntry);
}
std::vector myList;
std::字符串行;
while(std::getline(文件,行))
{
标准::istringstream iss(线);
列出我的中心;
//从临时输入流中读取逗号分隔的条目
std::getline(iss,myEntry.name,,);
std::字符串温度;
std::getline(iss,temp,',');
myEntry.age=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.birth=std::stoi(温度);
std::getline(iss,temp,',');
myEntry.id=std::stoi(temp);
myList.push_back(myEntry);
}

逗号有什么错误?它们包含在结果中?是的,我不希望它们包含在“name”或“age”变量中,因为我必须使用另一个函数显示节点。提前谢谢。您可以尝试搜索StackOverflow以查找“c++读取文本文件列表”对于过多的示例。我的错误是,您正在读取一个“逗号分隔值”文件,因此您需要在StackOverflow中搜索“C++读取文件csv”,并查看涉及文本字段的结果。我以前的建议是针对非csv文件(例如使用制表符)。谢谢@Thomas,这是一位朋友对我说的,要查找CSV文件,但我完全是一个乞丐,几乎不懂所使用的语法。任务说它必须是一个结构列表,变量必须分配给一个节点。换行是另一个新节点。搜索StackOverflow中的“C++读取文件CSV”将对我有所帮助:)再次感谢你!逗号有什么错误?它们是否包含在结果中?是的,我不希望它们包含在“name”或“age”变量中,因为我必须使用anot显示节点