如何从.txt文件中读取符号“/”和“:”的生日?

时间:2011-03-27 03:26:31

标签: c++ file symbols text-files

我正在编写c ++ prgram并且必须从.txt文件中读取一些数据。格式如下所示: “name gender DD/MM/YYYY HH:MM:SS

我知道如何阅读.txt文件并逐个删除这些字词,但我不知道如何处理符号“/”或“:”。<登记/>  我必须比较两个人的年龄。

请帮忙!

5 个答案:

答案 0 :(得分:2)

这是sscanf之类的任务之一:

sscanf(input_string, 
       "%s %s %d/%d/%d %d:%d:%d", 
       name, gender, &day, &month, &year, &hour, &minute, &second);

可以使用iostreams,但不是那么容易。你有几个基本的选择。一种是使用带有':'和'/'的locale定义为空格,这将允许您只是流入值并忽略分隔符(但不会警告您某些类型的错误格式,例如用'/'代替':',反之亦然。

另一种可能性是明确地读取这些字符,并且(如果您选择)通过将每个分隔符与您期望的内容进行比较来验证它们。如果你非常认真地确保数据符合预期的格式,那么这可能是最佳选择。

答案 1 :(得分:1)

请看以下链接。它会解决你的问题。

答案 2 :(得分:1)

在C中你可以直接使用sscanf()或更多,fscanf(),但这些函数在C ++中被认为是不好的样式。

我建议这样的事情:

struct SData 
{
    std::string sName;
    std::string sGender;
    int nDay;
    int nMon;
    int nYear;
    int nHour;
    int nMin;
    int nSec;
};

bool ReadRecord(std::istream &istr, SData &data)
{
    istr >> data.sName
         >> data.sGender
         >> data.nDay;
    istr.ignore(1);       // '/'
    istr >> data.nMon;
    istr.ignore(1);       // '/'
    istr >> data.nYear
         >> data.nHour;
    istr.ignore(1);       // ':'
    istr >> data.nMin;
    istr.ignore(1);       // ':'
    istr >> data.nSec;

    return istr.good();
}

<强>更新

以下示例通过排除struct

简化了使用
#include <iostream>
#include <fstream>
#include <string>

bool ReadRecord(std::istream &istr, 
                std::string &sName,
                std::string &sGender,
                int &nDay,
                int &nMon,
                int &nYear,
                int &nHour,
                int &nMin,
                int &nSec)
{
    istr >> sName
         >> sGender
         >> nDay;
    istr.ignore(1); // '/'
    istr >> nMon;
    istr.ignore(1); // '/'
    istr >> nYear
         >> nHour;
    istr.ignore(1); // ':'
    istr >> nMin;
    istr.ignore(1); // ':'
    istr >> nSec;

    return istr.good();
}

int main()
{
    std::string sName0, sGender0, sName1, sGender1;
    int nDay0, nMon0, nYear0, nDay1, nMon1, nYear1;
    int nHour0, nMin0, nSec0, nHour1, nMin1, nSec1;
    const char szFileName[] = "MyData.txt";

    std::ifstream istr(szFileName);
    if (!istr.is_open())
    {
        std::cerr << "Cannot open file\n";
        return 1;
    }
    if (!ReadRecord(istr, sName0, sGender0, nDay0, nMon0, nYear0, nHour0, nMin0, nSec0))
    {
        std::cerr << "Cannot read file\n";
        return 1;
    }
    if (!ReadRecord(istr, sName1, sGender1, nDay1, nMon1, nYear1, nHour1, nMin1, nSec1))
    {
        std::cerr << "Cannot read file\n";
        return 1;
    }
    std::string sYounger;

    if (nYear0 == nYear1)
    {
        if (nMon0 == nMon1)
        {
            if (nDay0 == nDay1)
            {
                if (nHour0 == nHour1)
                {
                    if (nMin0 == nMin1)
                    {
                        if (nSec0 > nSec1)
                        {
                            sYounger = sName0;
                        }
                        else if (nSec0 < nSec1)
                        {
                            sYounger = sName1;
                        }
                    } 
                    else if (nMin0 > nMin1)
                    {
                        sYounger = sName0;
                    }
                    else if (nMin0 < nMin1)
                    {
                        sYounger = sName1;
                    }
                }
                else if (nHour0 > nHour1)
                {
                    sYounger = sName0;
                }
                else if (nHour0 < nHour1)
                {
                    sYounger = sName1;
                }
            }
            else if (nDay0 > nDay1)
            {
                sYounger = sName0;
            }
            else if (nDay0 < nDay1)
            {
                sYounger = sName1;
            }
        }
        else if (nMon0 > nMon1)
        {
            sYounger = sName0;
        }
        else if (nMon0 < nMon1)
        {
            sYounger = sName1;
        }
    }
    else if (nYear0 > nYear1)
    {
        sYounger = sName0;
    }
    else if (nYear0 < nYear1)
    {
        sYounger = sName1;
    }

    if (sYounger.empty())
    {
        std::cout << "The ages are the same\n";
    }
    else
    {
        std::cout << sYounger << "is younger\n";
    }

    return 0;
}

答案 3 :(得分:0)

除了使用sscanf的最简单方法,例如Jerry describes,也可以使用iostream。只需从文件中读取您想要的数据,直到您点击分隔符,然后将其丢弃为虚拟char

char dummy;
myfile >> first_name >> last_name >> day >> dummy >> month >> dummy ... // and so on.

答案 4 :(得分:0)

How to split a string?的启发,我决定尝试一下。

鉴于以下数据:

文件: dates.txt

james male 01/02/1987 04:01:02
jerry male 11/06/1965 08:03:04
jon male 21/10/1977 12:05:06

以下应用程序解析文件的每一行,并将信息存储在名为 dude 的临时对象上,然后将其添加到 people_list 。解析日期/时间是通过strtok完成的,这是将字符串拆分为标记的很好的功能。最后,我们迭代矢量打印发现的内容。

#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

typedef struct _time
{
    int h; // hour
    int m; // minute
    int s; // seconds
} Time;

typedef struct _date
{
    int y; // year
    int m; // month
    int d; // day
} Date;

class Person
{
public:
    string name;
    string gender;
    Time time;
    Date date;
};


int main()
{
    ifstream in("dates.txt");

    vector<Person> people_list;

    string line;
    while (getline(in,line))
    {
        if (line.empty())
        {
            continue;
        }

        vector<string> tmp;
        istringstream iss(line);

        copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tmp));

        // Parsing name and gender (easy)
        Person dude;
        dude.name = tmp[0];
        dude.gender = tmp[1];

        // Date
        char delim_slash[] = "/";
        char* result = NULL;
        result = strtok(const_cast<char*>(tmp[2].c_str()), delim_slash);
        dude.date.d = atoi(result);
        result = strtok(NULL, delim_slash);
        dude.date.m = atoi(result);
        result = strtok(NULL, delim_slash);
        dude.date.y = atoi(result);

        // Time
        char delim_colon[] = ":";
        result = strtok(const_cast<char*>(tmp[3].c_str()), delim_colon);
        dude.time.h = atoi(result);
        result = strtok(NULL, delim_colon);
        dude.time.m = atoi(result);
        result = strtok(NULL, delim_colon);
        dude.time.s = atoi(result);

        people_list.push_back(dude);
    }

    for (int i = 0; i < people_list.size(); i++)
    {
        cout << "Name: " << people_list[i].name << endl;
        cout << "Gender: " << people_list[i].gender << endl;
        cout << "Date: " << people_list[i].date.d << "/" <<
                            people_list[i].date.m << "/" <<
                            people_list[i].date.y << endl;

        cout << "Time: " << people_list[i].time.h << ":" <<
                            people_list[i].time.m << ":" <<
                            people_list[i].time.s << endl << endl;
    }
}

<强>输出:

Name: james
Gender: male
Date: 1/2/1987
Time: 4:1:2

Name: jerry
Gender: male
Date: 11/6/1965
Time: 8:3:4

Name: jon
Gender: male
Date: 21/10/1977
Time: 12:5:6