Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文件填充结构_C++_File Io_Structure - Fatal编程技术网

C++ 从文件填充结构

C++ 从文件填充结构,c++,file-io,structure,C++,File Io,Structure,嘿,伙计们,我正在学校做一个项目,我需要用文本文档中的信息填充一系列指针: 4101 BRAEBURN_REG 1 0.99 101.5 4021 DELICIOUS_GDN_REG 1 0.89 94.2 4020 DELICIOUS_GLDN_LG 1 1.09 84.2 4015 DELICIOUS_RED_REG 1 1.19 75.3 4016 DELICIOUS_RED_LG 1 1.29 45.6 4167 DELICIOUS_RED_SM 1 0.89 35.4 4124 EMP

嘿,伙计们,我正在学校做一个项目,我需要用文本文档中的信息填充一系列指针:

4101 BRAEBURN_REG 1 0.99 101.5
4021 DELICIOUS_GDN_REG 1 0.89 94.2
4020 DELICIOUS_GLDN_LG 1 1.09 84.2
4015 DELICIOUS_RED_REG 1 1.19 75.3
4016 DELICIOUS_RED_LG 1 1.29 45.6
4167 DELICIOUS_RED_SM 1 0.89 35.4
4124 EMPIRE 1 1.14 145.2
4129 FUJI_REG 1 1.05 154.5
4131 FUJI_X-LGE 1 1.25 164.1
4135 GALA_LGE 1 1.35 187.7
4133 GALA_REG 1 1.45 145.2
4139 GRANNY_SMITH_REG 1 1.39 198.2
4017 GRANNY_SMITH_LGE 1 1.49 176.5
3115 PEACHES 1 2.09 145.5
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
94011 ORGANIC_BANANAS 1 0.99 56.3
所以我必须把它放到一个结构中。以下是我的功能:

bool readInventory(string filename)
{
   inputFile.open(filename);   //opening products file
   bool errors = true;

   if(!inputFile.fail()) // validate that the file did open
   {

   while (!filename)      // Dynamically creates array and fills with info 
      { 
          product *inventory = new product();
          inputFile >> inventory->plu;
          inputFile >> inventory->itemName;
          inputFile >> inventory->saleType;
          inputFile >> inventory->price;
          inputFile >> inventory->currentInventory;
          itemArray[counter] = inventory;
          counter++;
        cout << itemArray[14]<< endl;
      }

   }

   else
   {
      cout << "\nError, unable to open products.txt.\n";
      errors = false;
      return errors;
   }

}// ends readInventory

如果有人需要,这里有一个指向实际作业的链接:

!filename
没有意义,您正在对字符串求反。如果您知道将有多少项,您可以设置一个条件,例如
while(counter<(howManyItems))
。如果没有,可以先通过循环查找行尾字符,然后将while更改为for。

要读取文件中的所有行,请使用以下代码:

std::string line;
while (std::getline(inputFile,line))
{   
    std::istringstream iss(line);
    product *inventory = new product();
    iss >> inventory->plu;
    iss >> inventory->itemName;
    iss >> inventory->saleType;
    iss >> inventory->price;
    iss >> inventory->currentInventory;

    itemArray[counter] = inventory;
    ++counter;
}
此外,还应尝试删除全局变量定义(例如,
计数器
输入文件
),并将其作为函数局部变量或参数提供(或者在
计数器
的情况下,甚至可以合理地将其用作返回值:返回
0
-1
用于错误情况,以及读取的记录数)。此外,在分配新记录并将其分配给
itemArray[计数器]之前,您还需要检查
计数器
是否小于
大小

我建议至少使用
std::vector
来管理记录:

std::vector<product> itemArray;

// ... 

std::string line;
while (std::getline(inputFile,line))
{   
    std::istringstream iss(line);
    product newProd;
    iss >> newProd.plu;
    iss >> newProd.itemName;
    iss >> newProd.saleType;
    iss >> newProd.price;
    iss >> newProd.currentInventory;

    itemArray.push_back(newProd);
}
std::vector itemArray;
// ... 
std::字符串行;
while(std::getline(inputFile,line))
{   
标准::istringstream iss(线);
新产品;
iss>>newProd.plu;
iss>>newProd.itemName;
iss>>newProd.saleType;
iss>>新产品价格;
iss>>newProd.currentInventory;
itemArray.push_back(newProd);
}
不需要
计数器
使用此代码,
itemArray.size()
将显示从文件中读取的记录数。

我猜您的意思是

while (inputFile) 
而不是

while (!filename) 

while(!filename)
的用途是什么?这种情况永远不会出现。我不知道放在那里的是什么,我正在尝试读取整个文件。使用
inputFile
流从文件中读取(例如,使用
getline()
方法,将字符串放入
std::istringstream
并解析记录的每个字段(使用
>
运算符)。我的错误。我会解决。IntelliSense:重载函数的实例“std::basic\u ifstream::getline[with _Elem=char,_Traits=std::char\u Traits]”与参数列表匹配参数类型为:(std::string)对象类型为:std::ifstream我在while中的句点(inputFile.getline(line))处收到此错误,我收到此错误IntelliSense:istringstream iss(line)处不允许使用不完整类型iss@user3367260修复了我的示例中的
std::getline()
错误(抱歉).我留下来想办法给你包括正确的标题,请看。'你可以设置一个条件,比如while(counter<(howManyItems))'嗯!所有这些听起来都很笨拙,尤其是当你要从文件中读取记录时!这并不能真正回答OP的问题。
while (!filename)