//文本文件的基本操作
#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)