C++ 将文件读入结构c++;

C++ 将文件读入结构c++;,c++,arrays,struct,fstream,C++,Arrays,Struct,Fstream,我正在尝试将一个.txt文件读入该程序中的一个struct数组并显示内容。 该文件如下所示: 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

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

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

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


有人能告诉我有什么问题吗?我试过使用指针,但情况变得更糟了-初学者

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

问题是
SIZE
50
!它不是
15
。当您试图读取超过文件结尾的内容时,输入将不会被读取超过第16行。因此,索引
15
之后的变量将被取消初始化,这是未定义的


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

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

话虽如此,只要您正确地编写读取循环,夸大您拥有的记录数并没有什么错(如果您预先将数组设置得太大,除了浪费空间之外)。以下是编写循环的方法,即使您犯了“错误”,将15项改为50项:

#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.
#包括
#包括
#包括
使用名称空间std;
int main(){
常数int SIZE=50;
int i;
结构记录{
字符串名;
字符串secondname;
浮动测试1马克;
浮动中间标记;
浮动年鉴;
};
记录[大小];
ifstream in(“Data.txt”);
int recCount=0;//跟踪记录的实际数量
//循环,直到到达文件末尾,或者直到找到大小记录,
//以先到者为准
而(!in.eof()&&recCount>记录[recCount].firstname>>记录[recCount].secondname中
>>记录[recCount]。test1mark>>记录[recCount]。中期测试标记>>记录[recCount]。年度标记;
++记录;
}
//如果您有15个项目,那么recCount==15。


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

我相信我们不需要这个循环

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;
    }
}
#包括
#包括
#包括
使用名称空间std;
ifstream in(“Data.txt”);
常数int SIZE=15;
void debugPrint();
void loadData();
结构记录{
字符串名;
字符串secondname;
浮动测试1马克;
浮动中间标记;
浮动年鉴;
}记录[大小];
int main()
{
loadData();
debugPrint();
}
void debugPrint()
{
对于(int i=0;icout
for(int i=0;i
——你怎么知道你有50个数据项要读?@PaulMcKenzie这是一个作业问题,这在要求中。我不在你的班上,所以我只能笼统地回答。如果你计数,你没有50个项目,因此你的循环在第15个项目后读取垃圾数据。你不写“读取循环”就像这样。你应该循环直到文件结束,或者直到达到预设的限制,以先到者为准。对你来说,eof是第一位的,但你一直在循环。谢谢@PaulMcKenzieThank You!这就是我所要做的:)从设置了eof的流中提取不是UB。只是没有输入。UB插入uniniti将数据化到cout上。@DavidThomas完全正确:)ThanksBut@PaulMcKenzie
#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;
    }
}