简体   繁体   中英

Reading time entries from a text file and storing them into a 3D array so they are useable, in C++

I am currently trying to convert a program I have from PHP into C++ and improve it, however I am having trouble figuring out how to read the data so it is useable.

The text file looks something like this:

No. , user No.,    date,   time
1   , 1,         2013-12-12, 08:00
2   , 2,         2013-12-12, 08:10
3   , 1,         2013-12-12, 12:00
4   , 2,         2013-12-12, 12:01
5   , 2,         2013-12-12, 12:55
6   , 1,         2013-12-12, 13:00
7   , 1,         2013-12-12, 17:00
8   , 2,         2013-12-12, 17:20
9   , 1,         2013-12-13, 08:00
10  , 2,         2013-12-13, 08:10
11  , 1,         2013-12-13, 12:00
12  , 2,         2013-12-13, 12:01
13  , 2,         2013-12-13, 12:55
14  , 1,         2013-12-13, 13:00
15  , 1,         2013-12-13, 17:00
16  , 2,         2013-12-13, 17:20

My goal is to read the text file, and do calculations with the time entries, determining total time worked that day, overtime that day, and total time and overtime over the month.

So my question would be how to read the text file, so I can use the date and time entries.

EDIT. Here is what my code currently looks like. I am not very experienced with C++ itself, which is why I am remaking a program I allready made previously, with a language I am more familiar with.

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int lines() {
        int number_of_lines = 0;
        std::string line;
        std::ifstream myfile("data.txt");

        while (std::getline(myfile, line))
            ++number_of_lines;
        std::cout << "Number of lines in text file: " << number_of_lines << endl;
        return number_of_lines;
    }

    int main ()
    {
int Nr;
int name;
int year;
int month;
int day;
int hour;
int min;
int seconds;
     int row = lines();
    int myarray[16][8];
    string line;
    ifstream myfile ("data.txt");
    if (myfile.is_open())
    {
    for(int i=0; i<row; i++){
    getline (myfile,line);
    sscanf(line.c_str(), "%i, %i, %i-%i-%i, %i:%i", &Nr, &name, &year, &month, &day, &hour, &min);
    seconds=hour*60*60+min*60;
       myarray[i][1] = Nr;
       myarray[i][2] = name;
       myarray[i][3] = year;
       myarray[i][4] = month;
       myarray[i][5] = day;
       myarray[i][6] = hour;
       myarray[i][7] = min;
       myarray[i][8]=seconds;
    }
    myfile.close();
    for(int i=0; i<16; i++){
        for( int j=1; j<=8; j++){
        cout << myarray[i][j] << " ";
    }
        cout << endl;
    }}
    else cout << "Unable to open file";
    system("PAUSE");
    return 0;
}

Create a struct or class with fields named number, FirstName, LastName, Date and Time. Read the file line by line and create one instance of the struct or class above per line. Put all of those instances in a container of your choice, std::vector might be the easiest.

Then write some loops that iterate over the container and calculate whatever you want.

If you encounter problems doing that, post your best try and the problems.

Now that you have posted your code and it's not along the lines of my suggested answer, I'll try to help with the actual errors:

Your program is a wild mix of C and C++. It would be better to stay with one language, either C or C++.

sscanf takes a const char * as first parameter. If you want to create a const char* from an std::string, use the c_str() function:

sscanf(line.c_str(), "%d, %s, %s, %d-%d-%d, %d:%d", ...

Next, your program will crash, because you declared your first and last names to be integers. As that's not the case, it will not work. Fix your declarations next.

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