考研复试系列——第一节 文件操作
前言
在考研复试中有不少上机题目需要读写文件,并在此基础上进行其他的运算,因此文件操作是必须要掌握的,本篇文件对C++的文件操作进行总结整理,
并给出一些学校的上机例题。
基本操作
第一,文件的打开与关闭
#include<iostream>
#include<fstream> //引入头文件,包含文件读写操作
using namespace std;
int main()
{
/*
*ios::in 表示为读入而打开文件
*ios::out 表示为输出(写)而打开文件
*ios::ate 表示初始位置为文件尾
*ios::app 表示所有输出附加在文件尾
*ios::trunc 表示文件已存在则先删除文件
*ios::binary 表示以二进制方式打开文件
*/
fstream file("F:\\shangji\\wang.bin",ios::out | ios::app | ios::binary);//关于ofstream和ifstream这里没有说明
if(!file.is_open())//判断是否打开文件成功
{
cout<<"open file error!"<<endl;
exit(1);
}
else
{
cout<<"open file success!"<<endl;
}
file.close(); //关闭文件
return 0;
}
第二,读取文件中的数据
假如有一个文件file.in,其内容有两行,每行有3个整数,如下所示:
123 45 78
22 23 98
22 23 98
要求:1 读取文件所有内容并输出
2 读取文件的每一行并输出
3 读取文件中的每一个整数并输出
代码如下:
#include<iostream>
#include<fstream> //引入头文件,包含文件读写操作
using namespace std;
int main()
{
//读取文件中所有内容并输出
fstream file("F:\\shangji\\file.in");//打开文件file.in
if(!file.is_open())//判断是否打开文件成功
{
cout<<"open file error!"<<endl;
exit(1);
}
char ch;
while(!file.eof()) //判断是否到文件尾
{
ch = file.get();//每次读取一个字符
cout<<ch;
}
cout<<endl;
file.close(); //关闭文件
//读取文件中的每一行并输出
fstream file2("F:\\shangji\\file.in");//打开文件file.in
if(!file2.is_open())//判断是否打开文件成功
{
cout<<"open file error!"<<endl;
exit(1);
}
char buffer[1000];
while(!file2.eof())
{
file2.getline(buffer,100);
cout<<buffer<<endl;
}
file2.close();
//读取文件中的每一个整数并输出
fstream file3("F:\\shangji\\file.in");//打开文件file.in
if(!file3.is_open())//判断是否打开文件成功
{
cout<<"open file error!"<<endl;
exit(1);
}
int number;
while(!file3.eof())
{
file3>>number; //读入一个整数(如果文件中是浮点数也是一样的,只需更改类型为float或者double即可)
cout<<number<<" ";
}
cout<<endl;
return 0;
}
#include<iostream>
#include<fstream> //引入头文件,包含文件读写操作
using namespace std;
int main()
{
//读取文件中所有内容并输出
ofstream file("F:\\shangji\\file.in",ios::app);//打开文件file.in
if(!file.is_open())//判断是否打开文件成功
{
cout<<"open file error!"<<endl;
exit(1);
}
file<<"\nI am a line\n";
file<<"I am another line\n";
file.close();
return 0;
}
例题
题目一: 字符串处理
从string.in文件里读入两个字符串,字符串除了含有数字还可能包括 -减号 ,E, e, 小数点,相加之后输出到文件string.out中,如果是浮点型要求用
科学计数法表示。
sample input:
34.56
2.45e2
sample out:
2.7956e2
#include<iostream>
#include<fstream> //引入头文件,包含文件读写操作
#include<iomanip>
using namespace std;
int main()
{
//读入数据
ifstream file1("F:\\shangji\\string.in");
if(!file1.is_open())
{
cout<<"Error opening file";
exit(1);
}
float number_one,number_two;
file1>>number_one;
file1>>number_two;
file1.close();
//写入数据
ofstream file2("F:\\shangji\\string.out");
if(!file2.is_open())
{
cout<<"Error opening file";
exit(1);
}
float target = number_one + number_two;
file2<<setiosflags(ios::scientific)<<setprecision(4)<<target<<endl;
file2.close();
return 0;
}
从number.in中读入n个数,求出这n个数中的最小值和最大值以及它们的最大公约数,输出到文件number.out中。number.in中的第一行为n,
接下来为n个大于0的整数
sample input:
3
4 8 6
sample output:
4 8 4
#include<iostream>
#include<fstream>
using namespace std;
int gcd(int a,int b)//计算最大公约数
{
if(a == b)
return a;
int temp;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
while(a % b)
{
temp = b;
b = a % b;
a = temp;
}
return b;
}
int main()
{
ifstream file("F:\\shangji\\number.in");
if(!file.is_open())
{
exit(0);
}
int n;
file>>n; //读入n
int min,max;
file>>min;//读入第一个数
max = min;
for(int i=1;i<n;i++)
{
int temp;
file>>temp;
if(temp > max)
max = temp;
else if(temp < min)
min = temp;
}
file.close();
int yue = gcd(max,min);
//写入
ofstream file2("F:\\shangji\\number.out");
if(!file2.is_open())
{
exit(0);
}
file2<<min<<" "<<max<<" "<<yue<<endl;
file2.close();
return 0;
}
题目三:string.in文件中有一个字符串,读入该字符串到缓冲区,并统计该字符串中有多少字母,数字,空格和其他字符
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream file("F:\\shangji\\string.in");
if(!file.is_open())
exit(1);
char str[1000];
int i=0;
while(!file.eof())
str[i++] = file.get();
str[i] = '\0';
cout<<"字符串为:"<<str<<endl;
//统计各字符种类个数
int types[4];//分别表示字母,数字,空格,其它
memset(types,0,sizeof(int)*4);
i = 0;
while(str[i] != '\0')
{
if(isalpha(str[i])) //注释:isalnum是判断是否是字母或数字
types[0]++;
else if(isdigit(str[i]))
types[1]++;
else if(isspace(str[i]))
types[2]++;
else
types[3]++;
i++;
}
cout<<"字母个数为:"<<types[0]<<endl;
cout<<"数字个数为:"<<types[1]<<endl;
cout<<"空格个数为:"<<types[2]<<endl;
cout<<"其它个数为:"<<types[3]<<endl;
return 0;
}