gpt4 book ai didi

c++ - 使用 while(inf.good) 读取整数文件

转载 作者:行者123 更新时间:2023-11-30 02:57:15 30 4
gpt4 key购买 nike

<分区>

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

#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> 作为输出,当我添加其余代码时,核心转储出现了。我不确定这是传递问题还是我的变量是否以某种方式关闭,但如果有人能帮助我,那将意义重大。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com