這是一個把文字16進位轉換成二進位的小程式
1 #include <cstdlib>
2 #include <iostream>
3 #include <fstream>
4
5 using namespace std;
6
7 int main(int Argc, char *Argv[])
8 {
9 string FileNameIn(Argv[1]);
10 string Temp;
11 unsigned int Ch;
12
13 ifstream FileIn(FileNameIn.c_str());
14 ofstream FileOut((FileNameIn + ".out").c_str(), ios::binary);
15
16 cout << FileIn << endl;
17 while(FileIn >> Temp)
18 {
19 sscanf(Temp.c_str(), "%02X", &Ch);
20 FileOut << (char)Ch;
21 }
22 cout << FileIn << endl;
23
24 FileOut.close();
25 FileIn.close();
26
27 system("PAUSE");
28 return EXIT_SUCCESS;
29 }
我有一些問題
1.
我用 Dev-C++ 4.9.9.2 for mingw32 3.4.5 來進行編譯是成功的且正確執行。
但是我用 Microsoft Visual C++ 6.0 結果出現以下訊息:
Compiling...
main.cpp
C:\hex2bin\main.cpp(14) : error C2784: 'class std::reverse_iterator<_RI,
_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse
_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument
for '' from 'class std::basic_string,class std::allocator >'
C:\hex2bin\main.cpp(14) : error C2676: binary '+' :
'class std::basic_string,class std::allocator >' does not define this
operator or a conversion to a type acceptable to the predefined operator
C:\hex2bin\main.cpp(14) : error C2228: left of '.c_str' must have
class/struct/union type
C:\hex2bin\main.cpp(17) : error C2679: binary '>>' : no operator defined
which takes a right-hand operand of type 'class std::basic_string,class
std::allocator >' (or there is no acceptable conversion)
C:\hex2bin\main.cpp(17) : fatal error C1903: unable to recover
from previous error(s); stopping compilation
Error executing cl.exe.
這表示我不能這樣寫嗎?那為什麼 Dev-C++ 卻可以呢?
2.
第 16 行和第 22 行分別顯示 FileIn 物件執行前和執行後的指標。
結果是:
0x22fe84
0
如果第二個顯示是 0 那我第 25 行程式還有執行的意義在嗎?
謝謝大家
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.68.161.161
推
06/19 16:20, , 1F
06/19 16:20, 1F
→
06/19 16:37, , 2F
06/19 16:37, 2F
→
06/19 16:39, , 3F
06/19 16:39, 3F
推
06/19 16:40, , 4F
06/19 16:40, 4F
→
06/19 16:41, , 5F
06/19 16:41, 5F
→
06/19 16:42, , 6F
06/19 16:42, 6F
→
06/19 16:42, , 7F
06/19 16:42, 7F
→
06/19 16:43, , 8F
06/19 16:43, 8F
→
06/19 16:55, , 9F
06/19 16:55, 9F
→
06/19 17:12, , 10F
06/19 17:12, 10F
→
06/19 17:12, , 11F
06/19 17:12, 11F
→
06/19 17:13, , 12F
06/19 17:13, 12F
→
06/19 17:55, , 13F
06/19 17:55, 13F
→
06/19 17:56, , 14F
06/19 17:56, 14F
→
06/19 17:58, , 15F
06/19 17:58, 15F
推
06/19 22:48, , 16F
06/19 22:48, 16F
推
06/19 23:41, , 17F
06/19 23:41, 17F