文件与数据库

本文介绍了C++中fstream库用于文件操作的基本概念,包括ofstream、ifstream和fstream的使用,详细阐述了如何打开、关闭文件,以及二进制文件的读写。还提到了getline函数、substr方法和字符串查找功能,同时讲解了文件流的定位操作如seekg和seekp,以及tellg和tellp函数的作用。最后,讨论了在程序中暂停显示结果的方法。
摘要由CSDN通过智能技术生成
//文本文件的基本操作 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   
	string s="12345";
	ofstream f1("f.txt",ios::out);//为了输出而打开文件
	if(f1)//判断这个文件存不存在
	{
   
		f1<<s<<endl;
		f1.close();
	}
	else cout<<"write file error!"<<endl;
	//=====================================
	ifstream f2("f.txt",ios::in);//为了写入而打开文件
	if(f2)
	{
   
		f2>>s;
		cout<<s<<endl;
		f2.close();
	}
	else cout<<"read file error!"<<endl;
	return 0;
}

ofstream //文件写操作 内存写入存储设备
ifstream //文件读操作,存储设备读区到内存中
fstream //读写操作,对打开的文件可进行读写操作

1.打开文件
在fstream类中,成员函数open()实现打开文件的操作,从而将数据流和文件进行关联,通过ofstream,ifstream,fstream对象进行对文件的读写操作
函数:open()
参数: filename 操作文件名
mode 打开文件的方式
prot 打开文件的属性 //基本很少用到,在查看资料时,发现有两种方式
打开文件的方式在ios类(所以流式I/O的基类)中定义,有如下几种方式:
ios::in 为输入(读)而打开文件
ios::out 为输出(写)而打开文件
ios::ate 初始位置:文件尾
ios::app 所有输出附加在文件末尾
ios::binary二进制方式

2.关闭文件
当文件读写操作完成之后,我们必须将文件关闭以使文件重新变为可访问的。成员函数close(),它负责将缓存中的数据排放出来并关闭文件。这个函数一旦被调用,原先的流对象就可以被用来打开其它的文件了,这个文件也就可以重新被其它的进程所访问了。为防止流对象被销毁时还联系着打开的文件,析构函数将会自动调用关闭函数close。

3.二进制文件
在二进制文件中,使用<< 和>>,以及函数(如getline)来操作符输入和输出数据,没有什么实际意义,虽然它们是符合语法的。
文件流包括两个为顺序读写数据特殊设计的成员函数:write 和 read。第一个函数 (write) 是ostream 的一个成员函数,都是被ofstream所继承。而read 是istream 的一个成员函数,被ifstream 所继承。

//读取带空格的字符串
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   
	string s;
	ifstream f("test.txt",ios::in);//为写入而打开文件
	if(f)
	{
   
//		f>>s;//将内容s写入f中
		getline(f,s);
		cout<<s<<endl;
		f.close();
	}
	else cout<<"read file error!"<<endl;
	return 0;
}

getline函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。
getline 函数如下所示:getline(cin, inputLine);
其中 cin 是正在读取的输入流,而 inputLine 是接收输入字符串的 string 变量的名称。

//文件名作参数 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string read_file(string filename)
{
   
	ifstream f(filename.c_str(),ios::in);//c_str表示将文件名filename转换成c语言的形式
	string s="0";
	if(f)
	{
   
		getline(f,s);
		f.close();
	}
	else cout<<"open file error!"<<endl;
	return s;
}
int main()
{
   
	string filename;
	cout<<"input filename:";
	cin>>filename;
	cout<<read_file(filename)<<endl;
	return 0;
}
//读取并显示文本文件内容 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void read_file(string filename)
{
   
	ifstream f(filename.c_str(),ios::in);
	string s;
	if(f)
	{
   
		while(!f.eof())//eof()函数可以帮助我们用来判断文件是否为空,或是判断其是否读到文件结尾
		{
   
			getline(f,s);//getline函数中前面参数是文件,后面参数是读出内容
			cout<<s<<endl;
		}
		f.close();
	}
	else cout<<"open file error!"<<endl;
}
int main()
{
   
	string filename;
	cout<<"input filename:";
	cin>>filename;
	read_file(filename);
	return 0;
}
//英汉词典检索:文件操作 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class test
{
   
private:
	ifstream f;
	string s,s1,word;
	string::size_type n;//string::size_type在不同的机器上,长度是可以不同的,并非固定的长度。但只要使用了这个类型,就使得你的程序适合这个机器
//npos表示size_type的最大值,用来表示不存在的位置。
public:
	test()
	{
   
		f.open("英汉词典.txt",ios::in);
		if(!f)
		{
   
			cout<<"open file error!"<<endl;
			exit(0);//调用 exit 函数时,无论是哪个函数包含了该调用,都将导致程序停止
		}
	}
	void dict()
	{
   
		cout<<"input a word:";cin>>word;
		while(!f.eof())
		{
   
			getline(f,s);
			if(s.find(word,0)!=string::npos)
			{
   
				n=s.find(" ",0);
				s1=s.substr(0,n);
				if(s1==word)
				{
   
					cout<<s<<endl;
					break;
				}
			}
		}
	}	
	~test()//在结束的时候做收尾工作
	{
   
		f.close();
	}
};
int main()
{
   
	test t;
	t.dict();	
	return 0;
}

substr有2种用法:
假设:string s = “0123456789”;
string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”
string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”

//英汉词典检索:容器操作 
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;
class test
{
   
private:
	ifstream f;
	string s,s1,word;
	string::size_type n;
	map<string,string>a;
	map<string,string>::iterator p;
public:
	test()
	{
   
		f.open("英汉词典.txt",ios::in);
		if(!f)
		{
   
			cout<<"open file error!"<<endl;
			exit(0);
		}
		else 
		{
   
			while(!f.eof())
			{
   
				getline(f,s);
				n=s.find(" ",0);
				s1=s.substr(0,n);
				a.insert(pair<string,string>(s1,s));//first是指前面的,second是指后面的,检索
			}
		}
	}
	void dict()
	{
   
		cout<<"input a word:";cin>>word;
		p=a.find(word);
		if(p!=a.end())
		{
   
			cout<<p->second<<endl;//见上面的注释
		}
		else cout<<"no word!"<<endl;
	}	
	~test()
	{
   
		f.close();
	}
};
int main()
{
   
	test t;
	t.dict();	
	return 0;
}
//英汉词典检索:单串 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class test
{
   
private:
	ifstream f;
	string s,ss,s1,word;
	string::size_type n1,n2,n3,pos;
public:
	test()
	{
   
		f.open("英汉词典.txt",ios::in)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值