标签:
#ifndef __helpguy_h__ #define __helpguy_h__ #include <stdlib.h> #include <stdio.h> #include <unistd.h> void err_msg(const char* errMsg, ...); #endif // __helpguy_h__
#include "helpguy.h" #include <stdarg.h> #include <errno.h> #include <string.h> void exit_msg(const char* errMsg, ...) { va_list ap; va_start(ap, errMsg); int errno_save = errno; char buf[1024]; vsnprintf(buf, sizeof(buf) - 1, errMsg, ap); if(errno_save != 0) { int len = strlen(buf); snprintf(buf + len, sizeof(buf) - len - 1, ": (%d) %s", errno_save, strerror(errno_save)); } strcat(buf, "\n"); // output fflush(stdout); fputs(buf, stderr); fflush(stderr); va_end(ap); exit(1); }
#include "helpguy.h" #include <iostream> int main(int argc, char** argv) { std::cout << "Please enter positive integer: "; int value; std::cin >> value; if(value <= 0) exit_msg("need positive integer"); std::cout << "The value is: " << value << std::endl; std::cout << "OK" << std::endl; return 0; }
标签:
原文地址:http://blog.csdn.net/alex_my/article/details/43529323