이것은 내 코드입니다.
// This program demonstrates the use of flags.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename; bool tf; double number;
cout << "Name a file to create/overwrite: ";
cin >> filename;
ofstream outfile (filename.c_str());
if(outfile.fail())
{
cout << "Creating/Overwriting the file has failed.\nExiting...\n";
return 1;
}
cout << "Give me a boolean (0/1): "; cin >> tf;
cout << "Give me a large number with decimal points: "; cin >> number;
outfile.setf(ios_base::boolalpha); // Turns on boolalpha flag.
outfile << "Here's a boolean: " << tf << endl;
outfile.unsetf(ios_base::boolalpha); // Unsets boolalpha flag.
outfile << "Here's your number: " << number << endl;
outfile.setf(ios_base::scientific); // Turns on scientific notation flag.
outfile << "Here's your number is scientific notation: " << number << endl;
outfile.setf(ios_base::fixed); // When possible, floating point numbers will not appear in scientific notation.
outfile << "Here's your number in fixed notation: " << number << endl;
outfile.setf(ios_base::hex); // Numbers will appear in hexadecimal format.
outfile << "Here's your number in hexadecimal format: " << number << endl;
outfile.setf(ios_base::oct, ios_base::uppercase); // Numbers will appear in uppercase, octal format.
outfile << "Here's your number in octal format: " << number << endl;
return 0;
}
이걸 실행하면 ...
test.txt의 내용 :
Here's a boolean: false
Here's your number: 3491.67
Here's your number is scientific notation: 3.491670e+03
Here's your number in fixed notation: 3491.67
Here's your number in hexadecimal format: 3491.67
Here's your number in octal format: 3491.67
"hex"및 "oct"플래그를 설정할 때 왜 작동하지 않습니까?
텍스트 파일에서 "16 진수 형식 :"및 "8 진수 형식 :"옆에 "3591.67"이 아닌 다른 것을 예상했습니다.
플래그를 잘못 구현 했습니까?
불행히도 8 진법과 16 진법 인쇄는 정수에 대해서만 작동하고 double은 작동하지 않습니다. 참조 http://stdcxx.apache.org/doc/stdlibug/28-3.html를
setf를 사용하려면 다음과 같아야합니다.
outfile.setf(ios_base::hex,ios_base::basefield);
. 또는 std : hex의 파이프, 즉 :
outfile << std::hex;
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다