简体   繁体   中英

Reading from a file into an object?

I have a file of the format:

3 // num of tasks in taskset1
1 2 3 //priority for each task
10 20 30  //exec time, deadline , period
23 34.5 45
23 56 98
4 // num of tasks in taskset2
1 2 4 3//priority for each task
10 20 30  //exec time, deadline , period
23 34.5 45
23 56 98
34 54 100
......

I need to read the text file directly into a structure object taskset .

struct tasks
{
    double wcet;
    double deadline;
    double period;
    int priority;
};

struct tasksets
{
    tasks task[100];
    double num_tasks;
} taskset[100];

I am storing the contents in an array and then into objects which works fine. But since my file size is too huge, my program gets killed. I need to do without using the huge array. And instead of 100 different objects just use one object for the taskset . Any suggestions on how to do it?

int main(int argc, char* argv[])
{
    double aa[1000][1000];
    int u = 0;
    int v = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    string temp; // a temporary variable used to store string data
    vector<vector<string>> tokens;
    while (getline(cin, temp))
    {
        v = 0;
        istringstream iss(temp);

        vector<string> tokens_in_line;

        while (iss >> temp)
        {
            double temp1 = atof(temp.c_str());
            aa[u][v] = temp1;
            v++;
            tokens_in_line.push_back(temp);
        }
        u++;
        if (!tokens_in_line.empty())
        {
            tokens.push_back(tokens_in_line);
        }
    }
    cout << "Execution started" << endl;
    for (i = 0; i < u; i = i + j)
    {
        cout << "Task set #" << setnum << endl;
        taskset[setnum].num_tasks = aa[i][0];
        cout << "Number of tasks: " << taskset[setnum].num_tasks << endl;

        for (k = 0; k < taskset[setnum].num_tasks; k++)
        {
            taskset[setnum].task[k].priority = aa[i + 1][k];
            taskset[setnum].task[k].wcet = aa[i + 2 + k][0];
            taskset[setnum].task[k].deadline = aa[i + 2 + k][1];
            taskset[setnum].task[k].period = aa[i + 2 + k][2];
            cout << "Task " << k + 1 << ":";
            cout << " Priority : " << taskset[setnum].task[k].priority;
            cout << " WCET : " << taskset[setnum].task[k].wcet;
            cout << " Deadline : " << taskset[setnum].task[k].deadline;
            cout << " Period : " << taskset[setnum].task[k].period << endl;
        }
        j = k + 2;
        setnum++;
    }
    return 0;
}

Yeah you can read directly into the variables of the class with the operator >>. use getline to read into each variable. The code I used before is below for you to see ho its done. Basically you want to create a class with those variable needing to be read into and then you want to instanciate it in the class or main whichever you doing and read the file then add each variable that way. see below where p is the instanciated class with variables.

 string STRING;
    string floor;
    string toFloor;
    ifstream infile;
    infile.open("passenger requests.txt");

    if (!infile)
    {
        cout << "no good file failed! \n" << endl;
    }

    while (infile.good())
    {
        for (int i = 0; i < 49; ++i)
        {
            getline(infile, STRING);
            //Saves the line in STRING.
            infile >> p[i].time;

            getline(infile, floor, ',');
            infile >> p[i].fromFloor;
            getline(infile, toFloor, ',');
            infile >> p[i].toFloor;
        }

    }

    infile.close();
}

As Josh already pointed out, you can read your tokens into the variable directly instead of storing them in aa . By the way, aa being a double array of arrays is not a good choice for your integer tokens anyway.

Concerning the use of fixed-size arrays and crashing your program for too large input sets, I would instead go for either a std::list or std::vector for the tasksets and append new objects as needed.

Finally, your code doesn't compile as provided, I had to add a declaration for setnum and the necessary headers.

The main problem I see in your format - you have to read all priorities for all tasks in taskset before you can actually read tasks, if you can change format to store priority for each task at the same line where other details are - it would be better, anyway here is version which use less memory than your original one:

struct task {
    double wcet;
    double deadline;
    double period;
    int priority;
};

int main(int, char**) {
    task currentTask;
    std::string temp; // a temporary variable used to store string data
    while (std::getline(std::cin, temp)) {
        const int taskCount = atoi(temp.c_str()); // read number of tasks
        // parse task priorities
        int* priorities = new int[taskCount];
        for (int i=0; i<taskCount; i++)
            std::cin >> priorities[i];
        // read actual tasks
        for (int i=0; i<taskCount; i++) {
            std::cin >> currentTask.wcet;
            std::cin >> currentTask.deadline;
            std::cin >> currentTask.period;
            currentTask.priority = priorities[i];

            std::cout << "Task " << (i + 1) << ":";
            std::cout << " Priority : " << currentTask.priority;
            std::cout << " WCET : " << currentTask.wcet;
            std::cout << " Deadline : " << currentTask.deadline;
            std::cout << " Period : " << currentTask.period << std::endl;
        }
        delete[] priorities;
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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