我一直在做這個項目,與看起來像下面的輸入文件涉及:的strtod將返回0
A 0.0345
B 0.3945
...
Z 0.2055
基本上,我讀每一行,之後讀行了,我想拉只有雙出字符串。我試圖用strtod()
和每個人的例子似乎用它完美的罰款,但在我的例子,它只返回0。
這裏是我的代碼:
for (count = 0; count < 26; count++)
{
char buffer[100]; /* Buffer for the line read by fgets */
char *letters; /* Stores the string calculated by strtod */
double relFreq = 0.0;
if (fgets(buffer, 100, stream) != NULL)
{
relFreq = (double)strtod(buffer, &letters); /* Since the format is A 0.0000, we're just grabbing the double */
*(sampleOne + count) = relFreq; /* Add the relFreq to the array */
}
}
通過使用調試器,buffer
有正確的數據,例如A 0.0345\n
,但是當strtod()
返回一個值時,它總是0.任何幫助將不勝感激!
我不習慣任何這些功能(我不使用C),但也許你給「0.0345」而不是「0.0345」(帶領先空間)? – Mephy 2014-11-05 19:51:47
或者只是使用fscanf讓生活變得更簡單一些? – bph 2014-11-05 19:53:09
**如果您不必(並且可以明確解釋您爲什麼需要以及爲什麼它是正確的),請勿施放**!另外,如果你不使用'strtod'中的endptr(例如檢查錯誤),不要問它:通過0. – Deduplicator 2014-11-05 19:53:24