前言
fread是吼東西
應某人要求(大概)科普一下
fread
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;
char st[233];
char *Ch=st;
int main()
{
fread(st,1,233,stdin);
cout<<st<<endl;
cout<<*Ch<<endl;
}
可以用文件輸入,也可以直接輸並在最后加Ctrl+Z
(下面的空行是因為讀入了一個換行符)
fread基本格式:
fread(字符串,1,字符串大小,stdin);
*Ch一開始指向的是st[0],之后可以不斷*++Ch來往后跳
快速讀入
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;
char st[233];
char *Ch=st;
int getint()
{
int x=0;
while (*Ch<'0' || *Ch>'9') *++Ch;
while (*Ch>='0' && *Ch<='9') x=x*10+(*Ch-'0'),*++Ch;
return x;
}
int main()
{
fread(st,1,233,stdin);
cout<<getint()<<endl;
}
fwrite
用處並不是很大
fwrite(字符串,1,字符串長度,stdout);
快速輸出
把數字轉成字符串再反過來加進去(要加上空格/換行符)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;
char st[233];
int Len;
void putint(int x)
{
int a[233];
int i,len=0;
if (!x) len=1;
while (x)
{
a[++len]=x%10;
x/=10;
}
fd(i,len,1)
st[++Len]=a[i]+'0';
st[++Len]=' ';
}
int main()
{
Len=-1;
putint(1);
putint(2);
putint(233);
fwrite(st,1,Len,stdout);
}
真正的fread/fwrite
其實上面的都是假的
但是上面的很好寫
下面的不需要額外空間,但不能關文件
#include <bits/stdc++.h>
using namespace std;
namespace io
{
const int SIZE = 1 << 22 | 1;
char iBuf[SIZE], *iS, *iT, c;
char oBuf[SIZE], *oS = oBuf, *oT = oBuf + SIZE;
#define gc() (iS == iT ? iT = iBuf + fread(iS = iBuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++) : *iS++)
template<class I> void gi(I &x)
{
int f = 1;
for(c = gc(); c < '0' || c > '9'; c = gc())
if(c == '-') f = -1;
for(x = 0; c >= '0' && c <= '9'; c = gc())
x = (x << 3) + (x << 1) + (c & 15);
x *= f;
}
inline void flush()
{
fwrite(oBuf, 1, oS - oBuf, stdout);
oS = oBuf;
}
inline void putc(char x)
{
*oS++ = x;
if(oS == oT) flush();
}
template<class I> void print(I x)
{
if(x < 0) putc('-'), x = -x;
static char qu[55];
char *tmp = qu;
do *tmp++ = (x % 10) ^ '0'; while(x /= 10);
while(tmp-- != qu) putc(*tmp);
}
struct flusher{ ~flusher() { flush(); } }_;
}
using io :: gi;
using io :: putc;
using io :: print;
int main()
{
int x;
gi(x);
print(x);
putc('\n');
return 0;
}