我有這個簡單的代碼:C++:的std :: string問題
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream in("file.txt");
string line;
while (getline(in, line))
{
cout << line << " starts with char: " << line.at(0) << " " << (int) line.at(0) << endl;
}
in.close();
return 0;
}
它打印:
0.000000 0.000000 0.010909 0.200000 starts with char: 32
A 0.023636 0.000000 0.014545 0.200000 starts with char: A 65
B 0.050909 0.000000 0.014545 0.200000 starts with char: B 66
C 0.078182 0.000000 0.014545 0.200000 starts with char: C 67
...
, 0.152727 0.400000 0.003636 0.200000 starts with char: , 44
< 0.169091 0.400000 0.005455 0.200000 starts with char: < 60
. 0.187273 0.400000 0.003636 0.200000 starts with char: . 46
> 0.203636 0.400000 0.005455 0.200000 starts with char: > 62
/0.221818 0.400000 0.010909 0.200000 starts with char:/47
? 0.245455 0.400000 0.009091 0.200000 starts with char: ? 63
¡ 0.267273 0.400000 0.005455 0.200000 starts with char: � -62
£ 0.285455 0.400000 0.012727 0.200000 starts with char: � -62
¥ 0.310909 0.400000 0.012727 0.200000 starts with char: � -62
§ 0.336364 0.400000 0.009091 0.200000 starts with char: � -62
© 0.358182 0.400000 0.016364 0.200000 starts with char: � -62
® 0.387273 0.400000 0.018182 0.200000 starts with char: � -62
¿ 0.418182 0.400000 0.009091 0.200000 starts with char: � -62
À 0.440000 0.400000 0.012727 0.200000 starts with char: � -61
Á 0.465455 0.400000 0.014545 0.200000 starts with char: � -61
奇怪......我怎樣才能得到真正的string
的第一個字符?
在此先感謝!
@Martjn:當你*標記*您的問題用C++,沒有必要把僞標籤,如「C++:」在標題。 – dmckee 2010-08-16 15:37:04