繁体   English   中英

在C ++中从具有整数和字符串的文本文件中读取数据的最佳方法是什么?

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

对于我的comp sci类,我必须创建一个(简单地说)处理员工的程序。 到目前为止,我还没有任何问题。 但是,分配的最后一部分是“创建一个测试程序,该程序处理从文本文件输入的5个雇员的数组,然后将其作为雇员报告输出到屏幕上。” 我认为这毕竟相对容易,毕竟解析文本文档有多难。 问题是我需要同时输入字符串和整数,并且它们不是任何简单的模式或分隔符。 我必须输入数据的功能如下所示:

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..}

我从文本文档中输入的内容之一是:

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

我打算通过检测将结束每个输入的每个“,”来分离每个输入。 我总是可以对其进行硬编码,以便将某些输入(输入数字4、5、6、7、10、13、14、16、17、18、22、23和24)解析为int,但这不会看起来非常好,也许有更好的方法可以做到这一点。 有人有建议吗?

我随时可以提供所需的任何其他信息。

此解决方案定义了一个与逗号(,)相匹配的新空白。 我还定义了一个数据结构,但这是可选的。 最后,它使用ifstream流在结构内部获取数据。

以下文本提供了更多信息: 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;
}

注意:小心字符串字段内的逗号。

暂无
暂无

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

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