【问题标题】:What would be the best way to read data from a text file with ints and strings in c++?在 C++ 中从带有整数和字符串的文本文件中读取数据的最佳方法是什么?
【发布时间】:2014-10-11 01:37:41
【问题描述】:

对于我的计算机科学课程,我必须创建一个(简单地说)处理员工的程序。到目前为止,我没有任何问题。但是,作业的最后一部分是“创建一个测试程序,该程序处理从文本文件输入的 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)解析为整数,但这不会看起来非常好,可能有更好的方法来做到这一点。有人有什么建议吗?

我总是可以提供任何需要的额外信息。

【问题讨论】:

  • 我会认真考虑将所有这些参数放入struct 并传递对该struct 对象类型的引用。
  • 在“12345 Street1”之后小镇不见了?
  • 另一件事,我在想,如果我在 int 之前添加某种特殊字符(而不是 123,它将类似于 &123)并且在将其存储为字符串之前,我会检查 & 是否为字符串的第一个字符,如果是,它是一个 int。我不明白为什么会这样。绝对不是最佳做法,但对于专注于其他课程的作业,她应该接受。
  • 我认为您需要明确地对每种类型进行硬编码。毕竟在某些时候你必须把它们放在一个显式的 C++ 变量中,所以我认为你不能逃避它。即使您将此数据存储为std::strings(如在属性映射中),也应该逐个字段地进行单独验证。
  • 我觉得你的文件格式没问题。我认为您在问题中的建议(您说看起来不太好)是一个很好的建议。

标签: c++ text input logic


【解决方案1】:

此解决方案定义了一个新的与逗号 (,) 匹配的空白。我还定义了一个数据结构,但这是可选的。最后,它使用 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;
}

注意:注意字符串字段中的逗号。

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2011-04-10
    • 2010-09-09
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多