简体   繁体   English

文件输入到struct struct数组中

[英]file input into a struct struct array

I writing a program for school that needs to keep a calender of when assignments are due. 我为学校编写了一个程序,该程序需要记录作业的到期时间。 I need to keep the course name (eg cs162), a description of the homework and a due date into an array of structs with a struct for the date within the first struct. 我需要将课程名称(例如cs162),作业说明和截止日期保留在结构体数组中,其中第一个结构体中的日期为结构体。

struct dueDate{
    int mm[2];
    int dd[2];
    int yyyy[4];
};

struct Task{
    char course[MAX_CAP];
    char description[MAX_CHAR];
    dueDate dueDate;
};

The first thing I need to do is read in any assignments that may already exist in from an already created file. 我需要做的第一件事是从已经创建的文件中读取可能已经存在的所有分配。 The format of the file is this: 该文件的格式是这样的:

courseName; 课程名; description; 描述; mm/dd/yyyy (dueDate). mm / dd / yyyy(到期日)。

Here is the load function (problem is after the strcpy's trying to get the int into the dueDate struct): 这是加载函数(问题在strcpy试图将int放入DueDate结构中之后):

void loadDB(Task assignment[], int& size, char location[]){
    ifstream inTasks;
    char courseName[MAX_CAP];
    char fullDescription[MAX_CHAR];
    int mm;
    int dd;
    int yyyy;
    Task courseAssignment;

    cout << "inside" << endl;

    inTasks.open(location);
    while(!inTasks){
        cerr << "There is a problem with the path of the file " << location << "!";
        exit(1);
    }
    inTasks.get(courseName, MAX_CAP, ';');
    while(!inTasks.eof()){
        inTasks.ignore(MAX_CAP, ';');
        inTasks.get(fullDescription, MAX_CHAR, ';');
        inTasks.ignore(MAX_CHAR, ';');
        inTasks >> mm;
        inTasks.ignore(MAX_CAP, '/');
        inTasks >> dd;
        inTasks.ignore(MAX_CAP, '/');
        inTasks >> yyyy;
        inTasks.ignore(MAX_CAP, '\n');

        strcpy(courseAssignment.course, courseName);
        strcpy(courseAssignment.description, fullDescription);

        // PROBLEM
        // the warings are under the courseAssignment part
        // the error message is "expression must be a modifiable lvalue"
        courseAssignment.dueDate.mm = mm;
        courseAssignment.dueDate.dd = dd;
        courseAssignment.dueDate.yyyy = yyyy;

        addToDB(assignment, size, courseAssignment);

        inTasks.get(courseName, MAX_CAP, ';');
    }
}

Thank you. 谢谢。

Your problem is the declaration of the DueDate structure. 您的问题是DueDate结构的声明。 You declare yyyy as an array of 4 ints, mm as an array of 2 ints and so on. 您将yyyy声明为4个整数的数组,将mm声明为2个整数的数组,依此类推。 And at the part of the code you pointed you are trying to copy an int over an array of ints, which is impossible. 在您指出的代码部分中,您试图将int复制到int数组上,这是不可能的。

If you eliminate the array declaration part from the struct, it should work perfectly. 如果从结构中删除了数组声明部分,则它应该可以正常工作。

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

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