【问题标题】:Reading file into array of struct c++将文件读入struct c++数组
【发布时间】:2016-12-10 00:02:22
【问题描述】:

我正在尝试将 .txt 文件读入此程序中的结构数组并显示内容。
该文件如下所示:

Smith   Jack    60    45    98  
Harry   Hisk    45    40    78  
Kay     Jacob   35.5  23    45  
Dos      hed    23    20    35  
Noa      Tom    55    12    32  
Joe      Peni   57    49    78  
Vin      San    25.6  23    65.5  
Jes      Dan    24.3  12    78  
Zi       Lee    56    49    99  
Angi     Dev    57    48    97  
Donald   David  60    50    96  
Davis    Lal    47    47    80  
Alvis   Sen     56    46    85  
Jack    Jill    45    45    75  
Messy   Lionel  60    49    100  

我正在运行的代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
    const int SIZE=50;
    int i;
    struct Records {
        string firstname;
        string secondname;
        float test1mark;
        float midtestmark;
        float annualmark;
    }record[SIZE];

    ifstream in("Data.txt");

    if (!in){
    cerr << "File can't be opened! " << endl;
    system("PAUSE");
    exit(1);
    }
    for (int i=0; i < SIZE; i++){
    in >> record[i].firstname >> record[i].secondname 
    >>record[i].test1mark >> record[i].midtestmark >> record[i].annualmark ;
    }
    for (int i=0;i< SIZE;i++) {
        cout << record[i].firstname<<"  ";
        cout << record[i].secondname<<" ";
        cout << record[i].test1mark<<"  ";
        cout << record[i].midtestmark << "  ";
        cout << record[i].annualmark << "   ";
    }   
return 0;
} 

我得到的输出:

Smith   Jack    60      45      98  
Harry   Hisk    45      40      78  
Kay     Jacob   35.5    23      45  
Dos     hed     23      20      35    
Noa     Tom     55      12      32  
Joe     Peni    57      49      78  
Vin     San     25.6    23      65.5  
Jes     Dan     24.3    12      78  
Zi      Lee     56      49      99  
Angi    Dev     57      48      97  
Donald  David   60      50      96  
Davis   Lal     47      47      80  
Alvis   Sen     56      46      85  
Jack    Jill    45      45      75  
Messy   Lionel  60      49      100  
                nan     0       8.94237e-039  
                4.36192e-039    0       -2.3511e-038  
                0       0       -2.3511e-038  
                0       0       0  
                1.32253e-038    0       1.32251e-038  
                4.2039e-045     0       -2.11122e+037  
                1.32251e-038    0       3.21276e-039  
                1.4013e-045     0       -2.3511e-038  
                1.4013e-045     0       3.76158e-037  
                0       0       3.76158e-037  
                0       0       1.12104e-044  
                4.36195e-039    0       4.36194e-039  
                3.57331e-043    0       6.0615e-039  
                0       0       3.21276e-039  
                4.2039e-045     0       6.41272e-039  
                1.12104e-044    0       6.63812e-039  
                4.36205e-039    0       -2.75237e+038  
                0       0       6.59812e-039  
                6.63426e-039    0       1.4013e-045  
                0       0       6.47961e-039  
                3.21319e-039    0       3.21319e-039  
                6.59812e-039    0       3.21299e-039  
                8.40779e-045    2.24208e-044    6.01433e-039  
                6.6045e-039     0       2.54408e-029  
                0       0       6.6045e-039  
                0       0       6.43946e-039  
                5.88656e-039    0       -4.12495e+011  
                0       0       0
                5.88656e-039    0       2.54408e-029  
                nan     nan     6.43029e-039  
                0       0       0
                5.93823e-039    0       -4.12495e+011  
                0       0       0
                5.93823e-039    0       5.74532e-044  
                nan     nan     5.93837e-039  

进程在 0.05447 秒后退出,返回值为 0
按任意键继续 。 . .

谁能告诉我它有什么问题?我试过使用指针,但它变得更糟了。 -初学者

【问题讨论】:

  • for (int i=0; i &lt; SIZE; i++){ -- 你怎么知道你有 50 个数据项要读取?
  • @PaulMcKenzie 这是一个作业问题,这是要求。
  • 我不在你们班,所以只能笼统地回答。如果你数一下,你没有 50 个项目,因此你的循环在第 15 个项目之后读取垃圾数据。无论如何,您都不会像这样编写“读取循环”。您应该循环直到文件结束,或者直到达到预设限制,以先到者为准。对你来说,eof 先出现,但你一直在循环。
  • 感谢@PaulMcKenzie

标签: c++ arrays struct fstream


【解决方案1】:

您的文件有15 行,因此您只能读取15 行数据。您正在使用变量SIZE 来控制应该读取多少行。

问题是SIZE50不是15。当您尝试读取超过文件末尾时,输入将不会被读取超过第 16th 行。所以,索引15之后的变量会被未初始化,也就是undefined

要么将文件中的行数增加到50,要么将SIZE 更改为15

【讨论】:

  • 谢谢!这就是我所要做的:)
  • 从设置了 EOF 的流中提取不是 UB。只是没有输入发生。 UB 将未初始化的数据插入到 cout 中。
  • @DavidThomas 完全正确 :) 谢谢
【解决方案2】:

正如@Rakete1111 的另一个回答所说,您指定了太多项目,并且让读取数据的循环超过了文件中的实际项目数。

话虽如此,只要您正确编写读取循环,就夸大您拥有的记录数量确实没有任何问题(除了如果您将数组预设太大会浪费空间)。以下是编写循环的方法,即使您犯了“错误”,将 50 项而不是 15 项:

#include <iostream>
#include <string>
#include <iostream>

using namespace std;

int main(){
    const int SIZE=50;
    int i;
    struct Records {
        string firstname;
        string secondname;
        float test1mark;
        float midtestmark;
        float annualmark;
    };

    Records record[SIZE];

    ifstream in("Data.txt");

    int recCount = 0;  // keep track of actual number of records

    // loop until we reach the end of file, or until we hit SIZE records,
    // whichever comes first
    while (!in.eof() && recCount < SIZE)
    {
        in >> record[recCount].firstname >> record[recCount].secondname 
        >>record[recCount].test1mark >> record[recCount].midtestmark >> record[recCount].annualmark ;
        ++recCount;
    }

    // now recCount == 15 if you have 15 items.

Live Example

请注意,我们有一个 while 循环,它将一直读取直到达到限制 (50),或者我们到达文件末尾。

【讨论】:

【解决方案3】:

我相信我们不需要那个

int i;

一开始

#include <iostream>
#include <string>
#include <fstream>
using namespace std;



ifstream in("Data.txt");
const int SIZE = 15;
void debugPrint();
void loadData();

struct Records {
    string firstname;
    string secondname;
    float test1mark;
    float midtestmark;
    float annualmark;   
}record[SIZE];

int main()
    {
    loadData();
    debugPrint();
    }

void debugPrint()
{
    for (int i = 0; i < SIZE; i++) 
    {
        cout << record[i].firstname << "  ";
        cout << record[i].secondname << " ";
        cout << record[i].test1mark << "  ";
        cout << record[i].midtestmark << "  ";
        cout << record[i].annualmark << "  " <<endl;        
    }
    system("PAUSE");
}

void loadData()
{   
    for (int i = 0; i < SIZE; i++)
    {
        if (!in)
        {                                               
            cerr << "File can't be opened! " << endl;
            system("PAUSE");
        }

        in >> record[i].firstname >> record[i].secondname
        >> record[i].test1mark >> record[i].midtestmark >> record[i].annualmark;
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多