使用方法和cin类似,只需要将cin换成xszq::cin即可。

#ifndef IO_H_
#define IO_H_


#if defined(__unix)    \
 || defined(__unix__)  \
 || defined(__linux)   \
 || defined(__linux__)
#define IO_H_VERSION 1.1

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <cctype>
#include <string>

namespace xszq {
class istream {
public:
    istream();
    istream& operator>>(int&);
    istream& operator>>(int short&);
    istream& operator>>(int long&);
    istream& operator>>(int long long&);
    istream& operator>>(int unsigned&);
    istream& operator>>(int short unsigned&);
    istream& operator>>(int long unsigned&);
    istream& operator>>(int long long unsigned&);
    istream& operator>>(char&);
    istream& operator>>(char unsigned&);
    istream& operator>>(char*);
    istream& operator>>(std::string&);
    operator bool();
private:
    char *ptr;
    char *begin;
    bool lastSuccess;
} cin;

istream::istream() {
    ptr = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
    begin = ptr;
    lastSuccess = true;
}
#define integerUnsignedOperator(type)                          \
istream& istream::operator>>(type& var) {                       \
    var = 0;                                                    \
    while (*ptr && !isdigit(*ptr)) {++ ptr;}                    \
    if (!(*ptr)) {lastSuccess = false; return *this;}           \
    while (*ptr &&  isdigit(*ptr)) {var=var*10+*ptr++-48;}      \
    return *this;                                               \
}
#define integerSignedOperator(type)                                \
istream& istream::operator>>(type& var) {                       \
    bool isNegative = false;                                    \
    var = 0;                                                    \
    while (*ptr && !isdigit(*ptr)) {isNegative|=*ptr++=='-';}   \
    if (!(*ptr)) {lastSuccess = false; return *this;}           \
    while (*ptr &&  isdigit(*ptr)) {var=var*10+*ptr++-48;}      \
    if (isNegative) var = -var;                                 \
    return *this;                                               \
}

integerSignedOperator(int)
integerSignedOperator(int short)
integerSignedOperator(int long)
integerSignedOperator(int long long)
integerUnsignedOperator(int unsigned)
integerUnsignedOperator(int short unsigned)
integerUnsignedOperator(int long unsigned)
integerUnsignedOperator(int long long unsigned)
istream& istream::operator>>(char& var) {
    var = '\0';
    while (*ptr && isspace(*ptr)) {++ ptr;}
    if (!(*ptr)) {lastSuccess = false; return *this;}
    var = *ptr++;
    return *this;
}
istream& istream::operator>>(char unsigned& var) {
    var = '\0';
    while (*ptr && isspace(*ptr)) {++ ptr;}
    if (!(*ptr)) {lastSuccess = false; return *this;}
    var = *ptr++;
    return *this;
}
istream& istream::operator>>(char *var) {
    *var = '\0';
    while (*ptr &&  isspace(*ptr)) {++ ptr;}
    if (!(*ptr)) {lastSuccess = false; return *this;}
    while (*ptr && !isspace(*ptr)) {*var = *ptr; ++ var; ++ ptr;}
    *var = '\0';
    return *this;
}
istream& istream::operator>>(std::string& var) {
    var = "";
    while (*ptr &&  isspace(*ptr)) {++ ptr;}
    if (!(*ptr)) {lastSuccess = false; return *this;}
    while (*ptr && !isspace(*ptr)) {var += *ptr; ++ ptr;}
    return *this;
}
istream::operator bool() {
    return lastSuccess;
}
}
#else
#error "This header file only supports Unix/Linux."
#endif


#endif

dup2函数可以复制文件描述符,从而实现文件重定向。该函数通常可以用来重定向进程的程序的标准输入输出。
该函数原型如下:

int dup2 (int __fd, int __fd2);

如果返回值为-1,则表示重定向失败。
该函数将会把文件描述符__fd2替换为__fd。
下面是一个重定向标准输入输出的简单例子:

#include <unistd.h>
#include <fcntl.h>
int main() {
    int fd1 = open("input.txt", O_RDONLY), fd2 = open("output.txt", O_WRONLY);
    dup2(fd1, STDIN_FILENO);
    dup2(fd2, STDOUT_FILENO);
    return 0;
}

这样就可以把STDIN重定向到文件input.txt,STDOUT重定向到文件output.txt了。
补充一点:如果需要让输出文件自动创建,open函数需要加上O_CREAT标识:

open("output.txt", O_WRONLY | O_CREAT, 0777);

如果需要让文件自动清空,需要加上O_TRUNC标识:

open("output.txt", O_WRONLY | O_TRUNC);