簡體   English   中英

.txt文件中的結構數組

[英]Array of Structs from .txt file

有一個項目,我應該要求用戶輸入文件名,然后獲取該文件並構造一個結構數組。 我完全迷路了! 這是我到目前為止所擁有的

#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;
}

有人可以告訴我我走的路是否正確嗎? 我得到的打印結果甚至與文本文件都不接近。

姓名POS類別身高體重家鄉/高中/上大學
60 Josh Mann OL SO 6-4 300弗吉尼亞海灘/弗吉尼亞湖
64 Ricky Segers K / P FR 5-11185弗吉尼亞州/亨里克省Glen Allen
70布蘭登·卡爾(Brandon Carr)OL RS_SR 6-2 305弗吉尼亞州切薩皮克/西分公司/福克聯合軍事學院
53卡爾弗特·庫克(Calvert Cook)LB FR 6-0 250弗吉尼亞州諾福克/布克·T·華盛頓
51 Michael Colbert DE RS_SR 6-1230 Fayetteville,NC / EE史密斯
22 TJ Cowart CB RS_JR 5-9 190弗吉尼亞海灘/弗吉尼亞州/海洋湖
1雅克威爾·貝利(Jakwail Bailey)WR SO 5-11 185新澤西州哈登菲爾德/保羅六世
25安德烈·西蒙斯(Andre Simmons)S JR 6-0 205弗吉尼亞州洛頓/南縣/范德比爾特
34 Johnel Anderson RB FR 5-8 180 Sicklerville,NJ /保羅六世

這是用戶可以輸入的三個信息之一,但是它們都具有相同類型的信息。 我看過課本,已經上網搜尋了幾個小時,當用戶輸入文件而不僅僅是從文件開始時,我什么都找不到。 任何幫助或指示將不勝感激。

我立即發現一些錯誤。

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 / clas尺寸,它會更好地工作。 為什么不將它們制成字符串?

另外,讀取最后一位還有一些問題。 看來您必須尋找/來區分各個部分,但我看不到您這樣做。

總體而言,如果您可以更改輸入的格式,那么閱讀此內容將更加干凈,但我懷疑您無法這樣做。 與其他除法器一樣,逗號分隔的值非常易於讀取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM