Read data from file and store into an array of structs

后端 未结 2 1950
说谎
说谎 2021-01-24 20:58

So I need help creating a program that will open a file and read the data from the file into an array of structs and then calculate a variety of things like highest, lowest, ave

相关标签:
2条回答
  • 2021-01-24 21:07

    This should read all your data into the array, you'll want an incrementor to

    ifstream inStream; inStream.open("scores.txt");

     while (!inStream.eof())
     {
    
           inStream >> StudentData arr[SIZE];
    
     };
     inStream.close();
    
    0 讨论(0)
  • 2021-01-24 21:32

    My suggestion:

    1. Add an operator function to read one StudentData object from a stream.
    2. Add a while loop in main. In each iteration of the loop, read a StudentData
    std::istream& operator>>(std::istream& in, StudentData& st)
    {
        return (in >> st.studentID
                   >> st.first_name
                   >> st.last_name
                   >> st.exam1
                   >> st.exam2
                   >> st.exam3);
    }
    

    and in main:

    openInputFile(inFile, inFileName);
    
    size_t numItems = 0;
    while ( inFile >> arr[numItems] )
       ++numItems;
    

    At the end of that, you would have successfully read numItems items into arr.

    0 讨论(0)
提交回复
热议问题