C ++ - 032- file read and write

C + ± 032- document reader -2020-3-7

A method (The freopen)

//freopen方式读写文件
#include<iostream>
#include<cstdlib>
#include<cstdio>
 using namespace std;
 int main()
 {
 	int a,b;
 	freopen("sum.in","r",stdin);
 	freopen("sum.out","w",stdout);
 	cin>>a>>b;
 	cout<<a+b<<endl;
 	fclose(in);//关闭文件流
	fclose(out);//关闭文件流	
 	
 }

//sum.in and sum.out exe in the same file
sum.in

1 2

After sum.out // execution

3

Method Two (FILE *)

//FILE*  fopen方法读写文件
#include<iostream>
#include<cstdlib> 
#include<cstdio>
int main()
{
	int i,len=0,temp[100];
	FILE*in=fopen("a.txt","r");//指针指向输入文件
	FILE*out=fopen("b.txt","w");//输出文件格式
	for(i=0;!feof(in);i++)
	{
		fscanf(in,"%d",&temp[i]);//读取文件中的数据
		len++;
	} 
	for(i=0;i<len-1;i++)//写入文件
	  fprintf(out,"%d",temp[i]);
	fclose(in);//关闭文件流
	fclose(out);//关闭文件流	
	
}

a.txt

1 2 3 4 5 6 7

After b.txt // execution

1234567

FILE explanation: each file is used both to open up a zone in memory, used to store information about the file (such as file name, file status and current location of the file, etc.). This information is stored in a structure of the type of variable. The system has a defined structure type, named FILE. After the completion of reading and writing files need to use fclose function closes the file stream.

DESCRIPTION feof function: feof (in) in the file pointer. It has only two return values. Return 1 when it encounters end of file (EOF), 0 otherwise. Use line 9! feof (in) mean, if feof (in) EOF is not met, it has been implementing for loop.

Fprintf and fscanf functions described functions: fprintf and fscanf functions to read and write functions are formatted function, but to read and write the file object.

Method three (the fstream)

//fstream
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	int a,b,c;
	ifstream fin("in.txt");
	ofstream fout("out.txt");
	fin>>a>>b>>c;
	fout<<a*b*c<<endl;
	fin.close();
	fout.close();
	return 0;
}

in.txt

2 3 4

After out.txt // execution

24

Method IV (fread function)

fread function model: size_tfread (Buffer void *, size_t size, size_tcount, in the FILE *);
(. 1) Buffer: for receiving address data (pointer).
(2) size: the size of a single element. In bytes, not bits.
(3) count: the number of elements.
(4) in: providing data file pointer.
(5) Return Value: the number of elements read.
as follows:

//fread读取文件1
#include<iostream>
#include<cstdlib>
#include<cstdio>
char a[1100000];
int main()
{
	FILE*in=fopen("a.in","rb");
	FILE*out=fopen("a.out","w");
	int n=fread(a,1,1100000,in);
	for(int i=0;i<n;i++)
	fprintf(out,"%c",a[i]);
	return 0;
} 

a.in

1 2 3 4

After a.out // execution

1 2 3 4

fread () are read in accordance with the char type, so when the data type bit value read is converted to the value needed again. Reference program

as follows:

//fread读取文件2
#include<iostream>
#include<cstdio>
using namespace std;
FILE * in=fopen("i.txt","rb");
int mark=0;
char a[10];
int getnum()//将char类型转换为int类型
{
	int obj=0;
	while(!(a[mark]>='0'&&a[mark]<='9'))
	mark ++;
	while(a[mark]>='0'&&a[mark]<='9')
	obj=obj*10+a[mark++]-'0';
	return obj;
} 
int main()
{
	freopen("p.txt","w",stdout);//文件输出
	int s=1,i;
	fread(a,6,1,in);//第二、三个参数依据数组大小而定,够用就好
	for(i=0;i<3;i++)
	s*=getnum();
	cout<<s<<endl;
	return 0; 
}

i.txt

1 2 3 4 5

After p.txt // execution

6

Note that, regardless of the file read and write method, the last line should never write getchar () or system ( "pause") such statements, as these statements are awaiting further user input a character. The file reading and writing in the form of a program, do not need to pause.

Comparison of four file read and write methods Table

Read and write methods freopen FILE* fstream fread
Read and write speed <iostream>Cin and cout case of using these standard input and output streams will be slower; <stdio.h>the use fscanf and fprintf these specialized function of the speed will be faster to read and write files. C language-specific. Speed ​​will be faster. C ++ only. high speed. Called one of the most accurate fastest way to read files. Even accurate to the space can be read. Because it is fast to read the entire file all speeds.
used internal memory The former is larger, the smaller the latter Small Larger Larger

100,000 reads data (0 <= data <= 399) and re-write the file comparison tests in Table

Read and write methods freopen FILE* fstream fread
time cost The former the latter 0.06 0.34 0.06 0.06 0.06
Memory (KB) The latter the former 888 616 616 872 1300
Published 91 original articles · won praise 101 · Views 3310

Guess you like

Origin blog.csdn.net/weixin_41096569/article/details/104712040