2013-10-31 37 views
0

我有一個CSV數據,它包含此結構:如何在C++中將日期讀入結構?

2345678 Meier Hans 12.10.1985 2.4 
int, char[],char[], date,float 

我想要從這些數據類型此行一個結構,我必須把結構到一個數組,現在當我來讀日期,我應該在結構中使用什麼變量類型?以及如何將單日,月份和年份的數字讀入我的類型?有沒有一個拆分函數在每個點(「。」)分裂? 我是否應該使用「struct tm ts」並將當天的月份和年份分配給ts.constants? (ts_mday; ts_mmonth等)

我做了什麼至今:

#include <fstream> 
#include <stdio.h> 
#include <iostream> 
#include <ctime> 
using namespace std; 

int main() 
{ 
    struct studentendaten { 
     int matrnr; 
     char[] name; 
     char[] vorname;  
     struct tm ts;   
     ts.tm_mday; 
     ts.tm_mon; 
     ts.tm_year; 
     float note; 
    } 


    FILE * pFile; 
    int ch; 
    int n = 0; 
    pFile=fopen("studentendaten.txt","r"); 
    if (pFile==nullptr) perror ("Error opening file"); 
    else 
    {  
    while (ch != EOF) 
    { 
     ch = fgetc (pFile); 
     if (ch == ';') {n++;} 
     putchar(ch); 
    } 
    fclose (pFile); 
    } 


    return 0; 
} 
+2

我纔去到更多的肉的問題上取得了語法錯誤出你現有的代碼工作。編譯器很擅長告訴你它們在哪裏。 –

+0

若要將日期/時間「字符串」轉換爲二進制結構,您可能需要查看'strptime()'函數。 – alk

+0

你是說你的**逗號** - 分值文件中不包含逗號? –

回答

1

現在,當我來讀的日期,我應該在結構使用何種變量類型?

我會使用類型爲std::tm的類,它是日期的標準C類。

我該如何讀取單日,月份和年份的數字?

這就是問題所在。沒有一種標準的解釋日期的方法。日期根據某些文化慣例而有所不同。出於這個原因,日期的格式化,插入和提取通常被委託給語言環境的time_puttime_get方面。

您可以將此功能合併到提取器中,該提取器不僅檢查有效的流狀態(因爲插入將使用具體文件流類完成,而不是FILE),但也確定操作的成功。例如:

#include <fstream> 
#include <iterator> 
#include <locale> 
#include <ctime> 

struct date // a simple date class 
{ 
    std::tm ts; 
}; 

template <class CharT, class Traits> 
std::basic_istream<CharT, Traits>& 
    operator>>(std::basic_istream<CharT, Traits>& is, date& d) 
{ 
    if (!is.good()) 
     return is; 

    std::ios_base::iostate err = std::ios_base::goodbit; 
    try 
    { 
     typename std::basic_istream<CharT, Traits>::sentry s(is); 
     if (s) 
     { 
      std::istreambuf_iterator<CharT> o 
       = std::use_facet<std::time_get<CharT, 
        std::istreambuf_iterator<CharT>>>(is.getloc()).get_date(
        is, std::istreambuf_iterator<CharT>(), is, err, &d.ts); 

      if (o != std::istreambuf_iterator<char>()) 
       err |= std::ios_base::failbit; 
     } else 
     { 
      is.width(0); 
      return is; 
     } 
    } catch(std::ios_base::failure&) 
    { 
     // check the exception mask set the stream state 
    } 

    is.setstate(err); 
    is.width(0); 
    return is; 
} 

int main() 
{ 
    std::ifstream in("in.txt"); 
    date d; 
    in >> d; 
} 

這是提取器。插入器應該看起來有點相似,除了你會使用time_putistreambuf_iterator。您還將確保檢查插入失敗並設置badbit,如果是的話。

2

您可以使用strptime()來解析日期字符串並將其存儲在tm結構中。 tm結構包含日,月和年的字段。請記住,該月的日期從0到11,年份從1900開始。使用strftime()將tm結構轉換回字符串。

例子:

#include <string.h> 
#include <stdio.h> 
#include <ctime> 

int main(int argc, char** argv) 
{ 
    struct tm ts; 

    if(argc > 1) 
    { 
    memset(&ts, '\0', sizeof(ts)); 

    const char* cp = strptime(argv[1], "%d.%m.%Y", &ts); 

    if(cp != NULL) 
    { 
     char buf[16]; 
     strftime(buf, sizeof(buf), "%d.%m.%Y", &ts); 
     printf("%s\n", buf); 
    } 
    } 

    return 0; 
}