이렇게 보이는 것이 단순해야하지만 네트워크 검색에서는 찾을 수 없습니다.fail()이 true 일 때 ofstream을 열지 못하는 이유를 감지하십시오.
저는 Ofstream이 open()
이고 fail()
이 사실입니다. errno
과 같이 열지 못한 이유를 알고 싶습니다. sys_errlist[errno]
.
이렇게 보이는 것이 단순해야하지만 네트워크 검색에서는 찾을 수 없습니다.fail()이 true 일 때 ofstream을 열지 못하는 이유를 감지하십시오.
저는 Ofstream이 open()
이고 fail()
이 사실입니다. errno
과 같이 열지 못한 이유를 알고 싶습니다. sys_errlist[errno]
.
불행하게도,() 실패 이유를 정확하게 공개 찾는의 표준 방법이 없습니다. sys_errlist는 표준 C++이 아닙니다 (또는 표준 C, 나는 믿습니다).
strerror의 기능은 <cstring>
일 수 있습니다. 이것은 반드시 표준 또는 휴대용 아니지만, 우분투 상자에 GCC를 사용하여 나를 위해 괜찮 작동합니다
#include <iostream>
using std::cout;
#include <fstream>
using std::ofstream;
#include <cstring>
using std::strerror;
#include <cerrno>
int main() {
ofstream fout("read-only.txt"); // file exists and is read-only
if(!fout) {
cout << strerror(errno) << '\n'; // displays "Permission denied"
}
}
이 휴대용하지만 유용한 정보를 제공하기 위해 표시되지 않습니다 :
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ofstream;
int main(int, char**)
{
ofstream fout;
try
{
fout.exceptions(ofstream::failbit | ofstream::badbit);
fout.open("read-only.txt");
fout.exceptions(std::ofstream::goodbit);
// successful open
}
catch(ofstream::failure const &ex)
{
// failed open
cout << ex.what() << endl; // displays "basic_ios::clear"
}
}
우리가 표준 : : fstream을 사용할 필요가 없습니다, 우리는, 부스트 : iostream
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
void main()
{
namespace io = boost::iostreams;
//step1. open a file, and check error.
int handle = fileno(stdin); //I'm lazy,so...
//step2. create stardard conformance streem
io::stream<io::file_descriptor_source> s(io::file_descriptor_source(handle));
//step3. use good facilities as you will
char buff[32];
s.getline(buff, 32);
int i=0;
s >> i;
s.read(buff,32);
}
그럼이 화면이 실패하면 어떻게 표시됩니까? – paulm
잘 작동 할 수 사용 strerror()는 표준 C++ 함수입니다. 안타깝게도 표준에서는 open()이 errno를 설정한다고 명시하지 않으므로 절대적으로 의존 할 수는 없습니다. –
VS2013 업데이트 3에서 작동하는 것으로 보입니다. – paulm