将.txt中的数据输入到struct数组中

时间:2012-11-11 02:44:48

标签: c++ arrays struct

对于我的c ++作业,我有一个.txt文档,其中包含如下所示的建筑信息:

建筑物名称 一年建成 纬度坐标 离子坐标

离。

Parking Deck
1993
34.2252
37.5563
Admin Building
1985
34.2356
37.5734

我必须把它读成我创建的结构数组:

struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
} building;

现在我已经创建了一个for循环来读取数据到我创建的类型列表数组:

list buildingnumber[SIZE]; //array for buildings

但是当我尝试打印出“k”最早的建筑时,它在阵列中没有显示数据

这是我目前的代码:

#include <iostream>
#include <fstream>
#include <istream>
#include <cstdlib>
#include <string>

using namespace std;
const int SIZE = 5000;

//struct for building type
struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
}building;

list buildingnumber[SIZE]; //array for buildings

void InsertionSort(list buildingnumber[], int buildingsloaded)
{
    int key = 0, i = 0;
    for(int j = 1; j < buildingsloaded; j++)
    {
        key=buildingnumber[j].yearBuilt;
        i=j-1;
        while(buildingnumber[i].yearBuilt > key && i >= 0)
        {
            buildingnumber[i+1] = buildingnumber[i];
            i--;
        }
        buildingnumber[i+1].yearBuilt = key;
    }
}

int main()
{
    char filePath[50];
    ifstream openFile;
    cout << "Enter the path of the building file: ";
    cin.getline(filePath, 50);
    openFile.open(filePath);

    //verify if file is opened + report buildings loaded
    int buildingsloaded = 0;
    if(!openFile.fail())
    {
        while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
        {
            buildingsloaded++;
        }
        cout << buildingsloaded << " buildings have been loaded." << endl;
    }

    // get how many buildings user wants
    int k = 0;
    cout << "How many buildings are you interested in?: ";
    cin >> k;

    //create array
    // loadKbuildings(building, buildingsloaded);
    for(int i = 0; i < k; i++)
    {
        openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
    }

    // insertion sort
    InsertionSort(buildingnumber, buildingsloaded);

    // display earliest k buildings
    cout << "The " << k << " oldest buildings are: " << endl;
    int i = 0;
    while ( i < k )
    {
        cout << buildingnumber[i].name << endl;
        cout << "Year Built: " << buildingnumber[i].yearBuilt << endl;
        cout << "Coordinates: (" << buildingnumber[i].latCoord << "," << buildingnumber[i].lonCoord << ")" << endl;
        cout << endl;
        i++;
    }
}

1 个答案:

答案 0 :(得分:2)

问题在于:

if(!openFile.fail())
{
    while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
    {
        buildingsloaded++;
    }
    cout << buildingsloaded << " buildings have been loaded." << endl;
}

您已经阅读了所有数据(计算建筑物数量);下次你试图抓取数据时,它就消失了。

您可以通过在第一次扫描时将建筑物存储在数组中来解决此问题。

int c = 0;
if(!openFile.fail())
{
    // *
    while(openFile >> buildingnumber[c].name >> buildingnumber[c].yearBuilt >> buildingnumber[c].latCoord >> buildingnumber[c].lonCoord)
    {
        buildingsloaded++;
        c++;
    }
    cout << buildingsloaded << " buildings have been loaded." << endl;
}

根据WhozCraig的评论,这位明星下面的一行只用一个字来表示建筑物的名称;你应该使用cin.getline()来改变循环条件。

你显然也应该拿出下面的数据阅读部分:

//create array
// loadKbuildings(building, buildingsloaded);
for(int i = 0; i < k; i++)
{
    openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
}