本文整理汇总了C++中ofstream::rdstate方法的典型用法代码示例。如果您正苦于以下问题:C++ ofstream::rdstate方法的具体用法?C++ ofstream::rdstate怎么用?C++ ofstream::rdstate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ofstream
的用法示例。
在下文中一共展示了ofstream::rdstate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
bool
Control::write_ok(ofstream& out_file, char* out_filename, char* msg)
{
int state = out_file.rdstate();
if ( state & (ios::failbit | ios::badbit) ) {
strstream strm;
strm << "Error! Cannot write data to the file:" << endl;
strm << out_filename;
if ( msg != NULL ) {
strm << endl << msg;
}
strm << ends;
theUI->errMsg(0, strm.str());
return false;
}
return true;
}
示例2: StartUp
//start the socket server, listen to client
void HttpServer::StartUp()
{
if (g_show_log) {
outputFile.open("httpserver_log.txt", ios::out);
if ((outputFile.rdstate() & std::ifstream::failbit) != 0) {
cout << "Error opening 'httpserver_log.txt'" << endl;
g_openlog_fail = true;
}
DBG_ONLY("httpserver.g_port is:" << g_port);
}
#if defined(__WIN32__) || defined(__WIN64__)
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
cout << "fail to init socket !!!" << endl;
if (g_show_log)
DBG_ONLY("fail to init socket !!!");
return;
}
#endif
if ((serversocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("fail to create server socket !!!");
if (g_show_log)
DBG_ONLY("fail to create server socket !!!");
return;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(g_port);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int tmp = 1;
#if defined(__WIN32__) || defined(__WIN64__)
if (setsockopt
(serversocket, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp,
sizeof(int)) < 0) ;
#else
if (setsockopt
(serversocket, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(int)) < 0)
#endif
if (g_show_log)
DBG_ONLY("fail to set SO_REUSEADDR !!!");
struct linger so_linger;
so_linger.l_onoff = true;
so_linger.l_linger = 10; // wait 10 seconds to close, to avoid TIME_WAIT of client
#if defined(__WIN32__) || defined(__WIN64__)
if (setsockopt
(serversocket, SOL_SOCKET, SO_LINGER, (char *)&so_linger,
sizeof(so_linger)) < 0)
#else
if (setsockopt
(serversocket, SOL_SOCKET, SO_LINGER, &so_linger,
sizeof(so_linger)) < 0)
#endif
if (g_show_log)
DBG_ONLY("fail to set SO_LINGER !!!");
if (bind
(serversocket, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
perror("fail to bind address !!!");
if (g_show_log)
DBG_ONLY("fail to bind address !!!");
return;
}
if (listen(serversocket, 5) < 0) {
perror("fail to listen !!!");
if (g_show_log)
DBG_ONLY("fail to listen !!!");
return;
}
gIsRun = 1;
cout << "[ Server is running, bind to " << g_port << " .....]" << endl;
if (g_show_log)
DBG_ONLY("[ Server is running, bind to " << g_port <<
" .....]");
#if defined(__WIN32__) || defined(__WIN64__)
pthread_t thread_id;
pthread_create(&thread_id, NULL, socket_thread, NULL);
if (thread_id.p)
pthread_join(thread_id, NULL);
WSACleanup();
#else
daemon(0, 0);
pthread_t thread_id;
pthread_create(&thread_id, NULL, socket_thread, NULL);
if (thread_id != 0)
pthread_join(thread_id, NULL);
#endif
}