从.txt获取日期和时间

时间:2017-03-29 02:56:23

标签: c++

我试图从我拥有的文件中获取日期和时间,就像这样

"Run01"
"‹L˜^’·","ŽžŠÔŽ²","Äضޓú•t","Äضގž"
"70000 DIV","100ms (1ms/S)","17-03-25","06:16:15.49"

"Á¬ÈÙ","±ÝÌß","“dˆ³Ž²","ºÒÝÄ","½¹°ØݸÞ","•ÏŠ·”ä","µÌ¾¯Ä","̨ÙÀ"
"CH1-1","8956 H-SPEED","200mV","Detector11","OFF","-","-","OFF"
"CH1-2","8956 H-SPEED","200mV","Detector12","OFF","-","-","OFF"
"CH2-1","8956 H-SPEED","200mV","Detector21","OFF","-","-","OFF"
"CH2-2","8956 H-SPEED","200mV","Detector22","OFF","-","-","OFF"
"CH3-1","8956 H-SPEED","200mV","Detector31","OFF","-","-","OFF"
"CH3-2","8956 H-SPEED","200mV","Detector32","OFF","-","-","OFF"
"CH4-1","8956 H-SPEED","200mV","Detector41","OFF","-","-","OFF"
"CH4-2","8956 H-SPEED","200mV","Detector42","OFF","-","-","OFF"

"ŽžŠÔ[s]","CH1-1[V]","CH1-2[V]","CH2-1[V]","CH2-2[V]","CH3-1[V]","CH3-2[V]","CH4-1[V]","CH4-2[V]"

其中日期和时间位于2逗号(,)

之后的第3行

我对谷歌上的例子感到有些困惑。任何人都知道如何做到这一点?

编辑*我添加了一些我到目前为止所做的一切,

#include <iostream>
#include <fstream>
#include <string>

const int LINE = 4;

int draft(){
    std::ifstream fdate("DataFile0001/DataFile_B0001_info.txt");
    std::string s;

    for (int i = 1; i<= LINE; i++)
        std::getline(f,s);

    std::cout << s;
    return 0;
}

老实说,我不想编译和运行它,而只是root draft.cc,因为我将使用它来处理我的根文件。但是因为这更像是一个c ++问题而不是root。在这里问我认为更合适

1 个答案:

答案 0 :(得分:0)

for (int i = 1; i<= LINE; i++)
{
    std::getline(f,s);

    if(i == 3)
    {
        size_t pos = s.find_first_of(",");
        pos = s.find_first_of("," , pos+1);

        std::string date = s.substr(pos+2 , s.find_first_of("," , pos+1)-pos-3);

        pos = s.find_first_of("," , pos+1);

        std::string time = s.substr(pos+2 , s.length()-pos-3);

        std::cout << date << std::endl;
        std::cout << time << std::endl;
        break;
    }
}