C++ C++;将文本文件放入数组中。(新手)

C++ C++;将文本文件放入数组中。(新手),c++,C++,大家好,请提前回答新手问题,但我没有找到任何与我的问题相关的主题。我有一个产品的文本文件,如下所示: //编号//说明//价格 100个办公室座位102.99 200台224.99 300电脑桌45.49 400桌羊肉23.99 500书架89.49 我想阅读它并将其保存在一个数组中,稍后我将从中搜索产品并计算客户购买的总价格 尝试了一些类似于我发布的代码的东西,但我认为我的启动完全错了。如蒙指教,我将不胜感激 #include <iostream> #include <fst

大家好,请提前回答新手问题,但我没有找到任何与我的问题相关的主题。我有一个产品的文本文件,如下所示:

//编号//说明//价格

100个办公室座位102.99

200台224.99

300电脑桌45.49

400桌羊肉23.99

500书架89.49

我想阅读它并将其保存在一个数组中,稍后我将从中搜索产品并计算客户购买的总价格

尝试了一些类似于我发布的代码的东西,但我认为我的启动完全错了。如蒙指教,我将不胜感激

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
    ifstream file("Proionta.txt");
    if(file.is_open())
    {
        string myArray[15];
        for(int i=0;i<15;i++)
        {
            file>>myArray[i];

        }
    }
system ("pause");
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream文件(“Proionta.txt”);
if(file.is_open())
{
字符串myArray[15];
对于(int i=0;i>myArray[i];
}
}
系统(“暂停”);
}

我是否应该尝试创建一个函数来将代码放入其中?

让我们从记录建模概念开始,然后获取原语

要将文本行建模为记录:

下一步是为
记录重载
操作员>

struct Record
{
  unsigned int number;
  std::string  description;
  double       price;

  friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
  input >> r.number >> r.description >> r.price;
  return input;
}
使用上述代码,您可以在文件中读取:

std::vector<Record> database;
Record r;
while (my_text_file >> r)
{
  database.push_back(r);
}
编辑2:数组与向量 许多作业要求您对数据进行处理,如平均值或搜索。这通常要求您保存数据。
两种流行的容器(从学生的角度来看)是数组和
std::vector

阵列不是一个好的选择,因为对于文件I/O,您永远无法确定有多少条记录,而且阵列的容量是静态的(从不改变)。因此,您需要自己调整大小:

static const unsigned int initial_capacity = 16U;
Record database[initial_capacity];
unsigned int capacity = initial_capacity;
Record r;
unsigned int items_read = 0;
while (my_text_file >> r)
{
  if (items_read < capacity)
  {
    database[items_read] = r;
    ++items_read;
  }
  else
  {
     // Allocate new, larger array
     // Copy items from old array to larger array.
     // Update capacity variable.
     // Delete old array
     // Change database pointer to new array
  }
}
静态常量无符号整数初始容量=16U;
记录数据库[初始容量];
无符号整数容量=初始容量;
记录r;
无符号整数项\u read=0;
while(我的文本文件>>r)
{
如果(项目读数<容量)
{
数据库[items\u read]=r;
++所读项目;
}
其他的
{
//分配新的、更大的阵列
//将项目从旧阵列复制到更大的阵列。
//更新容量变量。
//删除旧数组
//将数据库指针更改为新数组
}
}

std::vector
的一个很好的特性是,你可以像数组一样访问它,它会根据需要自动增加容量。

std::string
使用stream操作符不一定会给你整行输入。如果你想这样做,我建议你仔细研究一下。我想我开始的时候完全错了。你知道吗k?你为什么这样认为?你能分享你所面临的问题的任何细节吗?因为问题目前的形式还不清楚。我必须创建一个函数,从.txt文件中读取数据并将其保存在数组中。之后,我必须创建第二个函数,在数组中搜索产品代码我还必须创建一个类,该类将添加每个客户将选择的产品的价格,并最终打印出来。(这是一个类项目)@ GoGeeYVAS欢迎使用堆栈溢出。请花时间阅读和参考来自这里的内容和如何使用的材料。这些问题太多了。您可以通过搜索Internet找到更多的“StAccOpjsC++读取文件数组”。.你是先搜索的吗?非常感谢你花时间回答我的问题并提供给我信息。我将尝试使用你提供给我的一些方法,并找出下一步要做的事情。
unsigned int number;
std::string  description;
double price;
while (my_text_file >> number >> description >> price)
{
  // Do something with number, description, price
}
static const unsigned int initial_capacity = 16U;
Record database[initial_capacity];
unsigned int capacity = initial_capacity;
Record r;
unsigned int items_read = 0;
while (my_text_file >> r)
{
  if (items_read < capacity)
  {
    database[items_read] = r;
    ++items_read;
  }
  else
  {
     // Allocate new, larger array
     // Copy items from old array to larger array.
     // Update capacity variable.
     // Delete old array
     // Change database pointer to new array
  }
}