前言

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;
}

C++fread/fwrite的基础用法的更多相关文章

  1. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  2. 那些年,坑死自己的事之fread/fwrite

    今天继续看牛人做过的东西,这个小程序并不大,加上相当多的注释行,才5000多行.这个小程序是在linux下实现的,之前自己也一直用vi来看并加以更加详细的注释,但是效率实在太低.于是将其转移到wind ...

  3. logstash安装与基础用法

    若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...

  4. elasticsearch安装与基础用法

    来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...

  5. BigDecimal最基础用法

    BigDecimal最基础用法 用字符串生成的BigDecimal是不会丢精度的. 简单除法. public class DemoBigDecimal { public static void mai ...

  6. linux缓冲的概念fopen /open,read/write和fread/fwrite区别

    fopen /open区别 UNIX环境下的C 对二进制流文件的读写有两套班子:1) fopen,fread,fwrite ; 2) open, read, write这里简单的介绍一下他们的区别.1 ...

  7. Linux read/write fread/fwrite两者区别

    Linux read/write fread/fwrite两者区别 1,fread是带缓冲的,read不带缓冲. 2,fopen是标准c里定义的,open是POSIX中定义的. 3,fread可以读一 ...

  8. Vue组件基础用法

    前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...

  9. Smarty基础用法

    一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...

随机推荐

  1. multiple datasource config

    Hi Harshit S. project structure: multiple datasource config as follows: step 1: step 2:add a datasou ...

  2. 编写Python脚本把sqlAlchemy对象转换成dict的教程

    编写Python脚本把sqlAlchemy对象转换成dict的教程 在用sqlAlchemy写web应用的时候,经常会用json进行通信,跟json最接近的对象就是dict,有时候操作dict也会比操 ...

  3. C# 文件打开对话框 图片fitter

    "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|""Windows Bitmap(* ...

  4. oracle expdp/impdp/可传输表空间

    oracle expdp/impdp/可传输表空间/及一些参数 Oracle data pump 导出操作能够将表.索引.约束.权限.PLSQL包.同义词等对象从数据库导出,并将它们保存在一种非文本格 ...

  5. Java ——继承

    本节重点思维导图 面向对象三大特性:封装.继承.多态 面向对象编程的第一原则:面向接口编程 继承 代码利用的方式,子类与父类之间的关系 语法 访问控制修饰符 class 类名 extends父类名{ ...

  6. netcore 使用redis session 分布式共享

    首先准备redis服务器(docker 和redis3.0内置的哨兵进行高可用设置) 网站配置Redis作为存储session的介质(配置文件这些略).然后可以了解一下MachineKey这个东西.( ...

  7. 不能将X*类型的值分配到X*类型的实体问题的解决方法

    今天在学习链表的过程中遇到了这个问题,我用如下方法定义了一个结构体,然后这个函数想要在链表头插入一个节点.但是在函数的最后一行却出现了报错:不能将MyLinkedList * 类型的值分配到MyLin ...

  8. centos7使用kubeadm搭建kubernetes集群

    一.本地实验环境准备 服务器虚拟机准备 IP CPU 内存 hostname 192.168.222.129 >=2c >=2G master 192.168.222.130 >=2 ...

  9. POJ 3259 Wormholes SPFA算法题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  10. 关于“如何只用2GB内存从20亿,40亿,80亿个整数中找到出现次数最多的数?”的一种思路

    小弟不才,只懂一些c#的皮毛,有一些想法, int32值范围大概在-20亿——20亿,按hashtable一个keyvalue占8B的设定来说,最大可以存储大约2.5亿个 数字-次数对. 那么,可以将 ...