zoukankan      html  css  js  c++  java
  • C++fread/fwrite的基础用法

    前言

    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('
    ');
    	return 0;
    }
    
  • 相关阅读:
    第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-2 session的基本原理与create命令的使用
    第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-1 zookeeper常用命令行操作
    3-3 zookeeper的作用体现
    3-2 zk客户端连接关闭服务端,查看znode
    Python(2.7.6) glob
    SonarQube(5.0.1) 环境的安装配置
    Maven(3.0.5) 环境的安装配置
    Linux 命令
    Linux
    Python(2.7.6) copy
  • 原文地址:https://www.cnblogs.com/gmh77/p/11795417.html
Copyright © 2011-2022 走看看