2016-02-11 58 views
0

我的輸入是從.csv和如下:閱讀從.csv的日期,並把它們放在INT

1135,"2015-07-19","2015-07-28",446 
1130,"2015-12-14","2015-12-21",458 
1105,"2015-04-01","2015-04-12",761 
1125,"2015-01-01","2015-01-11",692 

...等

,我需要把2015年,07和19個在3個不同的int變量(int年,月,日),所以我能夠計算出是否有特定的書籍可供出租。我一直在使用getline,但可惜getline只能用char和我不能將char轉換回int ...而沒有int我不能計算如果我的書是否可用。

這裏是我的代碼:

while(!fp2.eof()) 
{ 
    fp2.getline(copyNo,sizeof(copyNo),','); 
    fp2.ignore(1); 
    fp2.getline(dateOut.year,sizeof(dateOut.year),'-'); 
    fp2.getline(dateOut.month,sizeof(dateOut.month),'-'); 
    fp2.getline(dateOut.day,sizeof(dateOut.day),'"'); 
    fp2.ignore(2); 
    fp2.getline(dateDue.year,sizeof(dateDue.year),'-'); 
    fp2.getline(dateDue.month,sizeof(dateDue.month),'-'); 
    fp2.getline(dateDue.day,sizeof(dateDue.day),'"'); 
    fp2.ignore(1); 
    fp2.getline(borrowerNo,sizeof(borrowerNo)); 
} 

是有辦法,我可以改變我的getlines所以日期的數字放在整數?

回答

0

實測值的溶液:

作出的輸入正是因爲該.csv(YYYY-MM-DD)作爲字符串,改變了我的解析作爲1串並做如下:

while(!fp2.eof()) 
    { 
     fp2.getline(copyNo,sizeof(copyNo),','); 
     fp2.ignore(1); 
     getline(fp2,dateOut,'"'); 
     fp2.ignore(2); 
     getline(fp2,dateDue,'"'); 
     fp2.ignore(1); 
     fp2.getline(borrowerNo,sizeof(borrowerNo)); 
    } 

if(dateDue<today) 
相關問題