简体   繁体   English

C ++将数据读入结构数组

[英]C++ reading data into array of structures

I found a way to read the data into my array structs but i cannot control how many it reads. 我找到了一种将数据读取到数组结构中的方法,但是我无法控制读取的数据量。 It will read all 14 entries in the input file but after it has read the 14 it continues for a couple lines then stops. 它将读取输入文件中的所有14个条目,但是在读取了14个条目之后,它将继续运行几行,然后停止。 However, if i place the code to read data inside any loop whether while or for, it starts printing huge numbers instead of what is contained in the file. 但是,如果我放置代码以在任何循环内读取数据,无论是在一段时间内还是在循环内,它都会开始打印大量数字,而不是文件中包含的内容。 Does anyone know why this happens? 有谁知道为什么会这样吗? Is there a way around it 有办法解决吗

Input file looks as follows( exclude the dot): There are 14 sets of data in the file. 输入文件如下所示(不包括点):文件中有14组数据。 So, as shown below, set 1 would include georgia 76.4 10, then north carolina 95.5 20, and so on till 14 sets. 因此,如下所示,集合1将包括乔治亚州76.4 10,然后是北卡罗来纳州95.5 20,依此类推,直到14个集合。

Data inside input file looks like this: 输入文件中的数据如下所示:

georgia
76.4 10
north carolina
95.5 20
and so on.

Need to print data to the screen like: 需要将数据打印到屏幕上,例如:

georgia 76.4 10
north carolina 95.5 20
etc.

The problem lies in my code were i attempt and horribly fail to read into the input file. 问题出在我的代码中,我试图并且非常无法读取输入文件。

#include <iostream>
#include <fstream>
#include <cstdlib>

struct ATdata  
{
   string state;
   double miles;
   int shelters;
};

int readData( ifstream& input, struct ATdata data[] );

int main()
{
    ifstream input;
    char filename[256];
    ATdata data[14];
    int i;

    cout << "Enter input file name: ";
    cin >> filename;

    input.open( filename );

    if ( input.fail() )
    {
        cout << "Input file does not exist." << endl;
        exit(1);
    }

    readData( input, data );

    cout << i << endl;
    return(0);

}

int readData( ifstream& input, struct ATdata data[] )
{
    int i, j;

    getline( input, data[i].state, '\t' );
    input.ignore( 14, '\n' );
    input >> data[i].miles;
    input.ignore( 1, ' ' );
    input >> data[i].shelters;

    for ( j = 0; j < 14; j++ )
    {
        cout << data[j].state << data[j].miles << data[j].shelters << endl;
    }
}

I apologize for all the info trying to be as clear as can be as to not look more idiotic. 对于所有信息要尽量清楚,以免显得更加愚蠢,我深表歉意。 Thank you have a nice day. 感谢您有一个愉快的一天。

getline() takes an ostream& and a string . getline()带有一个ostream&和一个string Since your types are not string , it gives you an error. 由于您的类型不是string ,因此会出现错误。 I recommend reading in your input like this: 我建议您像这样阅读您的输入:

for (int i = 0; i < 14; i++ )
{
   input >> data[i].state >> data[i].miles >> data[i].shelters;
}

BTW, your state is a char , that's 1 character. 顺便说一句,您的state是一个char ,即1个字符。 Make it an std::string . 将其设为std::string

The best way to get a whole line is to use std::getline . 获得整行的最好方法是使用std::getline However, if you try to mix unformatted input with formatted input then it gets a bit tricky. 但是,如果您尝试将未格式化的输入与已格式化的输入混合使用,则会有些棘手。 You need lots of ignore calls and other guff. 您需要大量的ignore呼叫和其他问题。 I think it is always clearer to read a whole line at a time, and then use a stringstream to parse that line. 我认为每次读取整行然后使用stringstream解析该行总是更清晰。

Try this: 尝试这个:

size_t readData( ifstream& input, struct ATdata data[], size_t max_items )
{
    size_t i;

    for ( i = 0; i < max_items; )
    {
        ATdata item;
        getline(input, item.state);

        string line_2;
        if ( ! getline(input, line_2) )
            break;

        istringstream iss(line_2);
        if ( ! (iss >> item.miles >> item.shelters) )
        {
        // skip malformed entry (or you could abort instead)
            continue;
        }

        data[i++] = item;
    }

    return i;
}

Call it with: 调用它:

size_t num_items = readData(input, data, sizeof data / sizeof data[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM