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 に答える