简体   繁体   中英

What would be the best way to read data from a text file with ints and strings in c++?

For my comp sci class I had to create a program that (simply put) handles employees. Up until this point I have had no issues. However, the last part of the assignment is to "Create a Test Program that handles an array of 5 employees that are input from a text file and then output as an employee report to the screen." I thought this would be relatively easy, after all, how hard is it to parse a text document. The problem is that I need to input both strings and ints and they are not in any kind of simple pattern or separation. The function that I have to input data to looks like this:

void employeeType::setEmployeeType(string first, string middle, string last,
                               int month, int day, int year, int ID,
                               string street, string town, string state, int zip, string county, string country,
                               int home, int cell, string email,
                               int sMonth, int sDay, int sYear,
                               string oStreet, string oTown, string oState, int oZip, string oCounty, string oCountry,
                               int work, int salary) {//code here..}

And one of my inputs from the text document:

William, R, Smith, 8, 24, 1963, 555238911, 12345 Street1, State1, 77123, County1, Country1, 1112223333, 3332221111, email@email.com, 3, 19, 2007, 12345 oStreet1, oTown1, oState1, 77987, oCounty1, oCountry1, 2221113333, 75000

I was planning on separating each input by detecting every ", " which would end each input. I could always hard code it so that certain inputs (input numbers 4, 5, 6, 7, 10, 13, 14, 16, 17, 18, 22, 23, and 24) would be parsed as ints, but that would not look very good and there is probably a better way to do this. Does anyone have a suggestion?

I can always provide whatever extra information is needed.

This solution define a new white-space that match on comas (,). I also define a data structure, but that is optional. Finally, it use the ifstream stream to get data inside the structure.

More info in the following text: http://en.cppreference.com/w/cpp/locale/ctype_char

#include <iostream> //to output results
#include <vector>   //for the whitespace
#include <fstream>  //to open the file
#include <string>   //strings
using namespace std;//Avoid that in real projects

// Define a new type of white-space that mach comas (,)
struct my_whitespace : std::ctype<char> 
{
    static const mask* make_table()
    {
        // make a copy of the "C" locale table
        static std::vector<mask> v(classic_table(),  
            classic_table() + table_size);
        // these will be whitespace
        v[','] |=  space; 
        // space and tab won't be whitespace
        v[' '] &= ~space;
        v['\t'] &= ~space;
        return &v[0];
    }
    my_whitespace(std::size_t refs = 0) :
        std::ctype<char>(make_table(), false, refs) {}
};

// Data structure to save each line of the file (optional)
struct Data
{
    string first;
    string middle; 
    string last;
    int month; 
    int day; 
    int year; 
    int ID;
    string street; 
    //...
};

// Main function, in real project, get that outside the main.    
int main()
{
    // Open the file
    ifstream file ("file.txt", ios::in);
    // Set the white-space to mache comas
    my_whitespace *ws = new my_whitespace();
    file.imbue( std::locale(file.getloc(), ws));
    if (file.is_open())
    {
        Data d;
        // For each line of the file
        while (file)
        {
            char s; // To skip the first space after the coma.
            // Read one line of the file.
            file >> 
                d.first >> 
                s >> d.middle >> 
                s >> d.last >> 
                s >> d.month >> 
                s >> d.day >> 
                s >> d.year >> 
                s >> d.ID >> 
                s >> d.street //>>
                /*...*/
                ;
            // Print the result (optional)
            cout << d.first << endl;
            cout << d.middle << endl;
            cout << d.last << endl;
            cout << d.month << endl;
            cout << d.day << endl;
            cout << d.street << endl;
            //...
        }
    }
    file.close(); // This function delete ws
    // delete ws;

    return 0;
}

Note: Careful with comas inside a string field.

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