C++ 从C+中的文本加载并行数组时遇到问题+;
对不起,如果这个问题有重复的话,我只看到有人用C问这个问题。 我试图用外部数据文件中的数据加载并行数组,但当文件打开并开始加载时,我在输出中得到的只是一个空白控制台 有问题的代码是C++ 从C+中的文本加载并行数组时遇到问题+;,c++,C++,对不起,如果这个问题有重复的话,我只看到有人用C问这个问题。 我试图用外部数据文件中的数据加载并行数组,但当文件打开并开始加载时,我在输出中得到的只是一个空白控制台 有问题的代码是 include <iostream> #include <fstream> using namespace std; ... Void InventoryData::loadArrays() { ifstream partIn; partIn.open("inventoryPr
include <iostream>
#include <fstream>
using namespace std;
...
Void InventoryData::loadArrays()
{
ifstream partIn;
partIn.open("inventoryPricing.dat");
if (partIn.is_open())
{
//Prime Read
partIn >> partNum[invCount]
>> price[invCount];
while(!partIn.eof())
{
invCount++;
partIn >> partNum[invCount]
>> price[invCount];
} //END While
partIn.close();
} //END IF*/
else
{
invCount = -1;
cout<<"File failed to open."<<endl;
}
}
包括
#包括
使用名称空间std;
...
Void InventoryData::loadArrays()
{
干扰素;
公开部分(“inventoryPricing.dat”);
if(partIn.is_open())
{
//主读
partIn>>partNum[invCount]
>>价格[存货];
而(!partIn.eof())
{
invCount++;
partIn>>partNum[invCount]
>>价格[存货];
}//结束时
partIn.close();
}//如果结束,则结束*/
其他的
{
invCount=-1;
库特
它现在返回每个文件,但也返回一个附加文件
您的代码测试的eof()
不正确。您应该测试输入操作的成功与否,除非您已经有输入失败。正确的代码是:
if (ifstream partIn("inventoryPricing.dat"))
{
invCount = 0;
while (partIn >> partNum[invCount] >> price[invCount])
++invCount;
if (partIn.fail())
{
// could set invCount to -1 to avoid partial processing?
std::cerr << "File parsing failed after " << invCount << " entries\n";
}
}
else
{
invCount = -1;
std::cerr << "File failed to open.\n";
}
if(ifstream partIn(“inventoryPricing.dat”))
{
invCount=0;
而(partIn>>partNum[invCount]>>price[invCount])
++存货;
if(partIn.fail())
{
//能否将invCount设置为-1以避免部分处理?
这是您显示的代码的自然结果-即,如果文件未打开,则块中只存在要执行的输出语句。与其查找(未生成的)输出,不如(a)在调试器中查看代码运行,或者(b)在读取数据后立即打印数据。即cout