2016-04-16 85 views
1

有没有一种方法可以使用fscanf()从文件中读取HH:MM格式,并将其视为int?该文件的格式如下:fscanf读取小时格式

3 14:50 20.10 

是否有可能像做fscanf(fp, "%d ... %f, &a, &b, &c);b将有1450?

+2

看一看这个[问题](http://stackoverflow.com/questions/20450333/fscanf-with-colon-delimited-data) – sjsam

回答

4

恐怕你不能这样做。但是,您可以:

fscanf(fp, "%d %d:%d %f", &a, &b1, &b2, &c); 
b = b1 * 100 + ((b1 > 0) * 2 - 1) * b2; // in case b1, b2 are the different sign. 
+0

罚款,只要作品'B1 ','b2'是相同的符号。考虑'-14:50'。 – chux

+0

@chux但“小时格式”不包含负数 –

+0

虽然不太常见,但“小时格式”也用于表示负时间,特别是时区偏移量。 [示例](https://en.wikipedia.org/wiki/Newfoundland_Time_Zone)。直接应用此答案可能会导致错误的结果。 – chux