EOF循环读取到文件末尾

时间:2019-05-09 02:15:11

标签: c++ loops eof

此程序需要一个常量整数才能将数组大小设置为50,而我输入的文件的数组要小于该数组。当我通过程序读取文件时,在读取文件的所有内容后,它会打印全0,最大为50个大小限制。

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

const int MENU_AMOUNT = 50;

void getItems(string itemNames[], int quantities[], double prices[], int& size, ifstream& infile);

void outputItem(string itemNames[], int itemLen, double prices[], int priceLen);

int main()
{
  ifstream infile;
  string filename;
  string itemNames[MENU_AMOUNT];
  int quantities[MENU_AMOUNT];
  double prices[MENU_AMOUNT];
  int size = 0;
  int itemLen = 20;
  int priceLen = 10;

  cout << fixed << setprecision(2);
  cout << endl;

    do
      {
        cout << "Enter filename: ";
        cin >> filename;
        infile.open(filename.c_str());
      }
    while (!infile.is_open());

    getItems(itemNames, quantities, prices, size, infile);

    outputItem(itemNames, itemLen, prices, priceLen);

    return 0;
}

void getItems(string itemNames[], int quantities[], double prices[], int& size, ifstream& infile)
{
  do
    {
      for (int i = 0; i < MENU_AMOUNT; i++)
        {
          infile >> itemNames[size] >> prices[size] >> quantities[size];
          size++;
        }

    }
  while (!infile.eof());


  infile.close();

  return;
}

void outputItem(string itemNames[], int itemLen, double prices[], int priceLen)
{
  for (int i = 0; i < MENU_AMOUNT; i++)
    {
      cout << left << setw(itemLen) << itemNames[i];
      cout << "$";
      cout << right << setw(priceLen) << prices[i];
      cout << endl;
    }

  return;
}

我得到的输出是:

FriedOysterSkins    $     14.99
KelpShake           $      1.99
KrabbyPatty         $      1.25
ButteredBarnacle    $      7.99
CrunchyKelpDog      $      5.99
FreshSludgePudding  $      2.99
Pizza               $     12.99
SmallCoralBits      $     20.00
KelpRings           $     20.50
SailorsSurprise     $     60.00
SmallSeafoamSoda    $     20.25
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $632300596474856049734706189829338342549423743118279836536966762333211979446970678021416769400077740197104484712885971191235557010031413546520439315996546896105110742152763641640428494858125564065503899934408855258480937266943855951872.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00
                    $      0.00

如果我将常量整数更改为所需的数组(12)大小,则它将正确输出,但是我需要file函数的末尾才能正常工作,而不是更改全局变量。

0 个答案:

没有答案
相关问题