【问题标题】:Array of Structs from .txt file.txt 文件中的结构数组
【发布时间】:2012-11-13 23:07:50
【问题描述】:

有一个项目,我应该要求用户输入文件名,然后获取该文件并制作一个结构数组。我完全迷路了!这是我到目前为止所拥有的

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

string filename;
ifstream inFile;

struct ftballPlayer
{
    int NO;
    string first;
    string last;
    char POS;
    char clas;
    int height;
    char ds;
    int iheight;
    string hometown;
};

int counter=0;
const int MAX_PLAYER=50;
void printList(const ftballPlayer list[],  int listSize);
void printOne ( ftballPlayer two);
void getData(ifstream& inFile, ftballPlayer list[], int& listSize);

int main ()
{
    ftballPlayer list[MAX_PLAYER] ;
    cout << "Enter the name of the input file:  " ;
    cin >> filename;

    inFile.open(filename.c_str ()  );

    if (!inFile)
    {
        cout<<"Cannot Open Input File."<<endl;
        cout<< "Program Terminates!"<<endl;
        return 1;
    }

    inFile.ignore (200,'\n');
    getData (inFile,  list, counter);

    for ( counter = 0;counter < 50;counter++)
    {
        printOne (list[ counter] ) ;
        cout  <<endl;
    }

    return 0;
}

void getData(ifstream& inFile, ftballPlayer list[], int& listSize)
{
    ftballPlayer item ;
    listSize = 0;

    inFile >> item.NO >> item.first >> item.last
           >> item.POS >> item.clas >> item.height
           >> item.ds >> item.iheight >> item.hometown;

    while (inFile )
    {
        list [listSize ] = item ;
        listSize++;

        inFile >> item.NO >> item.first >> item.last
               >> item.POS >> item.clas >> item.height 
               >> item.ds >> item.iheight >> item.hometown;
    }

    inFile.close () ;

}

void printList(const ftballPlayer list[],  int listSize)
{
    int looper;

    for ( looper = 0; looper <  listSize ; looper++)
    {
        printOne ( list [looper]  );
        cout << endl ;
    }
}

void printOne ( ftballPlayer one)
{  
    cout << fixed << showpoint << setprecision (2);
    cout << "NO " << one.NO;
    cout << setw(5) << left << "  Name: " << one.first << " " << one.last;

    cout << "  POS " << one.POS << setw(5);
    cout << "Class "<<one.clas<<setw (5);
    cout << "Height "<<one.height<<" "<<one.ds<<" "<<one.iheight<<setw(5);
    cout << "Hometown " << one.hometown << endl;
}

谁能告诉我我是否走在正确的轨道上?我得到的打印输出甚至与这个文本文件都不接近。

没有姓名 职位 班级 身高 体重 家乡/高中/上一所大学
60 Josh Mann OL SO 6-4 300 Virginia Beach, Va./Ocean Lakes
64 Ricky Segers K/P FR 5-11 185 Glen Allen, Va./Henrico
70 Brandon Carr OL RS_SR 6-2 305 Chesapeake, Va./Western Branch/Fork Union Military Academy
53 Calvert Cook LB FR 6-0 250 Norfolk, Va./Booker T. Washington
51 Michael Colbert DE RS_SR 6-1 230 Fayetteville, N.C./E.E.史密斯
22 T.J. Cowart CB RS_JR 5-9 190 Virginia Beach, Va./Ocean Lakes
1 Jakwail Bailey WR SO 5-11 185 Haddonfield, N.J./保罗六世
25 Andre Simmons S JR 6-0 205 Lorton, Va./South County/Vanderbilt
34 Johnel Anderson RB FR 5-8 180 Sicklerville, N.J./保罗六世

这是用户可以输入的三种信息之一,但它们都具有相同类型的信息。我查看了我的教科书,并且已经在网上搜索了几个小时,当用户输入文件而不是从文件开始时,我找不到任何关于这样做的信息。任何帮助或方向将不胜感激。

【问题讨论】:

  • 请发布您当前的输出。

标签: c++


【解决方案1】:

有几件事我马上就看错了。

struct ftballPlayer
{
       int NO;
       string first;
       string last;
       char POS; // Should be multiple characters
       char clas; // Should be multiple characters
       int height;
       char ds;
       int iheight;
       string hometown;
};

我怀疑,如果您的 POS/class 尺寸合适,效果会更好。为什么不把它们做成字符串呢?

此外,读取最后一位存在一些问题。看起来你必须寻找 / 来区分各个部分,但我不认为你这样做。

总体而言,如果您可以更改输入的格式,阅读此内容会更清晰,但我怀疑您无法做到。逗号分隔的值非常容易阅读,就像任何其他分隔符一样。

【讨论】:

  • 我尝试在第一次运行时让它们成为字符串,并得到了相同的结果。
  • 我想我快到了,我将 POS 和 class 更改为字符串,这有助于我在家乡和学校线路上卡住。就像一线弗吉尼亚海滩是家乡,大洋湖是学校。我为学校添加了一个字符串,但由于弗吉尼亚为家乡和海滩打印出的空间,为学校打印出。如何让字符串包含空格?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2021-02-15
  • 2022-01-05
相关资源
最近更新 更多