C++fread/fwrite的基础用法

摘要:
前言fread是吼东西应某人要求(大概)科普一下fread#include#include#include#include#definefofor#definefdforusingnamespacestd;charst[233];char*Ch=st;intmain(){fread;cout˂˂st˂˂endl;cout˂˂*Ch˂˂endl;}可以用文件输入,也可以直接输并在最后加Ctrl+Zfread基本格式:fread;*Ch一开始指向的是st[0],之后可以不断*++Ch来往后跳快速读入#include#include#include#include#definefofor#definefdforusingnamespacestd;charst[233];char*Ch=st;intgetint(){intx=0;while*++Ch;whilex=x*10+,*++Ch;returnx;}intmain(){fread;cout˂˂getint()˂˂endl;}fwrite用处并不是很大fwrite;快速输出把数字转成字符串再反过来加进去#include#include#include#include#definefofor#definefdforusingnamespacestd;charst[233];intLen;voidputint{inta[233];inti,len=0;if(!x)len=1;while{a[++len]=x%10;x/=10;}fdst[++Len]=a[i]+'0';st[++Len]='';}intmain(){Len=-1;putint;putint;putint;fwrite;}真正的fread/fwrite其实上面的都是假的但是上面的很好写下面的不需要额外空间,但不能关文件#includeusingnamespacestd;namespaceio{constintSIZE=1˂˂22|1;chariBuf[SIZE],*iS,*iT,c;charoBuf[SIZE],*oS=oBuf,*oT=oBuf+SIZE;#definegc()(iS==iT?
前言

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

C++fread/fwrite的基础用法第1张

(下面的空行是因为读入了一个换行符)

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

免责声明:文章转载自《C++fread/fwrite的基础用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Guava之缓存Delphi常用字符串函数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C++指针类型间强制转换

深入理解指针类型间的转换 C++中指针的强制转换 强制类型转换(int)、(int&)和(int*)的区别 内存中的地址   地址的本质就是一串0和1的机器代码,内存中的地址没有明确数据类型,但地址值有类型,以32位编译器为例,内存中的地址是一个32位的整数。无论什么类型的指针变量,在内存中本质上都是一样的,都是一个整数值的地址值,该地址值可以...

fwrite文件写入数据

文件的操作就两种:读和写 读:把文件中的内容读入到程序中,然后根据自己的项目需求把文件的数据进行相关的处理。 写:就是将程序中的数据,写入到文件中,去更新文件。 这么两种操作归到代码中就是这两种函数:一组是写函数,一组是读函数。 读和写是成对出现的,fwrite和fread是一对,fputs和fgets 是一对。这里所说的一对的意思是:一对中的读和写对文...

C#与C++数据类型比较及结构体转换[整理]

//c++:HANDLE(void   *)                          ----    c#:System.IntPtr//c++:Byte(unsigned   char)                     ----    c#:System.Byte//c++:SHORT(short)                   ...

常用文本压缩算法及实现(To be finshed!)

当前仅仅完成了一小部分, 程序上仅仅实现了普通的基于字符的huffman压缩与解压缩. 程序管理上尝试了使用cmake构建,还是很方便的. 测试实验了采用 google test 1.4,也是很好用的. 文档编辑尝试使用latex+cjk, latex2html,相当好用:) 恩,下一步先尝试 python嵌入c++,利用pygraphviz从而可以打印生...

android 性能測试iozone篇

一:简单介绍 iozone是一个文件系统的benchmark工具, 用于測试不同的操作系统中文件系统的读写性能, 能够測试下面13种模式 0=write/rewrite 1=read/re-read 2=random-read/write 3=Read-backwards 4=Re-write-record 5=stride-read 6=fwrite/...

大端模式与小端模式

一、介绍  超过一个字节的数据在内存中会用几个字节存储,根据数据在内存的存放方式,分大端模式和小端模式。 大端模式是将数据的高位存在内存的低位地址;小端模式而是将数据的高位存在内存的高位地址,以下是十六进制0x12345678在内存地址的两种存储方式(假设数据是按原码存储),0x12345678中,12是高位,78是低位。 大端模式中把高位(12)存在内...