1

我正在尝试打开一个整数文件,该文件将被传递到一个包含数组的结构中,但是当我尝试这样做时,我在输出中得到一个加零,当我在我的程序中添加更多时,核心只是被转储了,所以我不确定我做错了什么以及如何解决它。

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

struct Hand
{
  int handCards[52];
  int totalCards;
};

struct Card
{
  char rank;
  char suit;
};

void OpenFile (ifstream&, string&);
void ReadFile (ifstream&, Hand&);
void ProcessRank (Hand&, int CardRank[]);
void ProcessSuit (Hand&, int CardSuit[]);
char GetRank (int);
char GetSuit (int);
void PrintCard (Card);
Card ConvertRaw (Hand);
void PrintHand (Card, Hand);
int main()
{
  ifstream inf;
  string filename;
  Hand theHand;
  Card aCard;
  int CardRank[13];
  int CardSuit[4];

  OpenFile(inf, filename);
  ReadFile(inf, theHand);



}

void OpenFile (ifstream &inf, string &filename)
{
  cout<<"What is the name of the file?" <<endl;
  cin>>filename;

  inf.open(filename.c_str());

  if (inf.fail())
    {
      cout<<"Sorry, that file doesn't exist" <<endl;
      exit(1);
    }
  else
    cout<<"Success!" <<endl <<endl;
}

void ReadFile (ifstream &inf, Hand &theHand)
{
  theHand.totalCards=0;
  int i=0;
  while(inf.good())
    {

      inf>>theHand.handCards[i];
      theHand.totalCards++;
      cout<<theHand.handCards[i];
      i++;
    }
}

该文件是 123456,但随后我得到 1234560> 作为输出,当我添加其余代码时,核心转储出现了。我不确定传递是否有问题,或者我的变量是否以某种方式关闭,但如果有人可以帮助我,这将意味着很多。

4

1 回答 1

3

通常你想检查你的阅读尝试是否成功:

while(inf>>theHand.handCards[i])
{
  theHand.totalCards++;
  cout<<theHand.handCards[i];
  i++;
}

当然,您通常也应该使用std::vector而不是数组,但我想我们可以把它留给另一个问题。

于 2013-02-09T00:13:23.440 回答