I can't get my program to read my file in a function

时间:2022-03-25 22:54:07

I can't seem to figure out why my code is not reading in the data to use in a switch case. When I write it to a file, it just pulls up garbage. Can anyone help?

我似乎无法弄清楚为什么我的代码没有读取在交换机案例中使用的数据。当我把它写到一个文件时,它只是扯垃圾。有人可以帮忙吗?

void readData(Name element[], int size)
{
    ifstream infile("treeData.txt");

    int index = 0;
    string line, common, scientific, family;
    int name;

    infile.open("treeData.txt");
    {           
        {
            while((index < size) && (infile >> name >> common >> scientific >> family))
            {
                if(name >= 0 && name <= 100)
                {
                    infile >> element[index].treeID;
                    element[index].treeID = name;
                    infile >> element[index].commonName;
                    element[index].commonName = common;
                    infile >> element[index].scientificName;
                    element[index].scientificName = scientific;
                    infile >> element[index].familyName;
                    element[index].familyName = family;
                    index++;
                    size = index;
                }   
                else
                    cout << "The file was not found!";
            }
        }
    }       
    infile.close();
}

1 个解决方案

#1


2  

Your implementation should take advantage of the extensibility features of the C++ IOStreams library. You can create an overload of operator >> so that any input stream can extract data into a Name object. It's also advised that instead of extracting the data into an array (like you've attempted in your readData function) you extract it into a single object. That way code can be built on top of this functionality. It's also a more logical and straighforward way of performing extraction:

您的实现应该利用C ++ IOStreams库的可扩展性功能。您可以创建operator >>的重载,以便任何输入流都可以将数据提取到Name对象中。还建议不要将数据提取到数组中(就像您在readData函数中尝试过的那样),而是将其提取到单个对象中。这样,代码可以构建在此功能之上。它也是一种更合理,更直接的提取方式:

std::istream& operator>>(std::istream& is, Name& n)
{
    if (!is.good())
        return is;

    int id;
    std::string line, common, scientific, family;

    if (is >> id >> common >> scientific >> family)
    {
        if (id >= 0 && id <= 100)
            n.treeID = id;

        n.treeID         = name;
        n.commonName     = common;
        n.scientificName = scientific;
        n.familyName     = family;
    }
    return is;
}

Now that we have our extractor, we can go ahead and create an array of Name objects and use the extractor for each element:

现在我们有了提取器,我们可以继续创建一个Name对象数组,并为每个元素使用提取器:

std::ifstream infile("treeData.txt");
std::array<Name, 5> names;

for (auto name : names)
{
    infile >> name;
}

#1


2  

Your implementation should take advantage of the extensibility features of the C++ IOStreams library. You can create an overload of operator >> so that any input stream can extract data into a Name object. It's also advised that instead of extracting the data into an array (like you've attempted in your readData function) you extract it into a single object. That way code can be built on top of this functionality. It's also a more logical and straighforward way of performing extraction:

您的实现应该利用C ++ IOStreams库的可扩展性功能。您可以创建operator >>的重载,以便任何输入流都可以将数据提取到Name对象中。还建议不要将数据提取到数组中(就像您在readData函数中尝试过的那样),而是将其提取到单个对象中。这样,代码可以构建在此功能之上。它也是一种更合理,更直接的提取方式:

std::istream& operator>>(std::istream& is, Name& n)
{
    if (!is.good())
        return is;

    int id;
    std::string line, common, scientific, family;

    if (is >> id >> common >> scientific >> family)
    {
        if (id >= 0 && id <= 100)
            n.treeID = id;

        n.treeID         = name;
        n.commonName     = common;
        n.scientificName = scientific;
        n.familyName     = family;
    }
    return is;
}

Now that we have our extractor, we can go ahead and create an array of Name objects and use the extractor for each element:

现在我们有了提取器,我们可以继续创建一个Name对象数组,并为每个元素使用提取器:

std::ifstream infile("treeData.txt");
std::array<Name, 5> names;

for (auto name : names)
{
    infile >> name;
}