将文件读入结构数组

时间:2016-02-29 01:42:16

标签: c++ arrays file struct

在没有eof的情况下将文件读入结构时遇到麻烦。我在.h文件中包含所有必需的包含,并且收到错误消息“IntelliSense:no operator”>>“匹配这些操作数”

struct Courses
{
    string mCourseID;
    double mCourseNumber;
    double mMaxCapacity;
    double mCurrentEnroled;
};

Courses addCourse(istream &File);


Courses addCourse(istream &File)
{
    Courses sData;

    File >> sData.mCourseID;
    File >> sData.mCourseNumber;
    File >> sData.mCurrentEnroled;
    File >> sData.mMaxCapacity;

    return sData;
 }

void readCourses(Courses sCourses[], ifstream &File, int &numCourses)
{
    while (addCourse(File) >> sCourses[numCourses])
    {

        numCourses++;
    }
}

1 个答案:

答案 0 :(得分:3)

我想,您想要将addCourse的签名更改为此...

istream& addCourse(istream &File, Courses& sData)
{
    File >> sData.mCourseID;
    File >> sData.mCourseNumber;
    File >> sData.mCurrentEnroled;
    File >> sData.mMaxCapacity;

    return File;
}

然后,使用这些修改修改您的其他功能......

void readCourses(Courses sCourses[], ifstream &File, int &numCourses)
{
    while (addCourse(File, sCourses[numCourses]))
    {
        numCourses++;
    }
 //......

但同样,您的数组sCourses是否足以容纳您输入的Courses个数量?或者你会停止条件? ......重新考虑你的程序逻辑。