C++中文件的基本概念
C++中,文件可以被看做是一个连续的字符串集合,这个字符串集合没有大小。C++中,字符串是以流的形式存在的,因此,文件也可以看做是一个流的集合,被称为流式文件,增加了文件处理的灵活性。
C++对文件的操作可以概括为以下几个步骤:
1.建立文件流对象
2.打开或建立文件
3.进行读写操作
4.关闭文件
对于文件进行I/O操作的流类主要有fstream(输入输出文件流)ifstream(输入文件流)ofstream(输出文件流),这三个类都包含在头文件fstream中。
若ifstream对象重读使用,需注意在使用之前调用clear函数,否则会出现错误
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char buffer[256];
fstream out;
out.open("com.txt", ios::in);
cout << "com.txt" << endl;
while (!out.eof())
{
out.getline(buffer, 256, '\n');
cout << buffer << endl;
}
out.close();
cin.get();
}
这一段程序的任务是读取com.txt中的内容,但是要注意的一点是文件必须和cpp程序放在同一个文件夹当中或者标明文件的储存路径
ios::in的意思为以制度的方法打开文件
eof表示的是在文件的最后,操作系统会以一个eof表示文件的结束,在这里的用法
就是在读取到eof的时候结束对文件的输出
cin.get()用来读取回车键,防止输出内容闪过
文件的读写顺序
在C++的文件中,每一条记录是一个接一个的存储的。在这样的文件中,如果想要找到一条记录,就必须从文件的开头逐一读取文件的记录,直到找到该条记录的位置为止。
随机文件读写
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int CountLines(char* filename)
{
ifstream Readfile;
int n = 0;
string tmp;
Readfile.open(filename, ios::in);
if (Readfile.fail())
{
return 0;
}
else
{
while (getline(Readfile, tmp))
{
n++;
}
return n;
}
Readfile.close();
}
string Readline(char* filename, int line)
{
int lines, i = 0;
string temp;
fstream file;
file.open(filename, ios::in);
lines = CountLines(filename);
if (line <= 0)
{
return "Error1";
}
if (file.fail())
{
return "Error2";
}
if (line > lines)
{
return "Error3";
}
while (getline(file, temp) && i < line - 1)
{
i++;
}
file.close();
return temp;
}
void main()
{
int l;
char filename[256];
cout << "Please enter the document name" << endl;
cin >> filename;
cout << "\n Please enter the lines you want to read" << endl;
cin >> l;
cout << Readline(filename, l);
cin.get();
}
利用这段程序,就可以读取任意一个文件中的任意一行内容
文件的打开与关闭
在之前的例子当中实际上已经进行过一些文件打开和关闭的相关操作了,接下来是用更系统的方式对文件的打开和关闭的相关内容进行介绍
C++中,要进行文件的输入或输出,必须创建一个流,把这个流与文件相关联,才能对文件进行操作,在完成之后要关闭文件。
fstream中,有一个成员函数open用来打开文件,原型为:
void open(const char* filename, int mode, int access)
filename是要打开的文件名
mode是打开文件的方式
access是打开文件的属性
方式在类ios(也是所有流式I/O类的基类)中定义,常用的有
ios::app 以追加的方式打开文件
ios::ate 文件打开后定位到文件尾。 ios::app中就包含这个属性
ios::binary 以二进制的方式打开文件,默认方式是文本方式
ios::in 文件以输入方式打开
ios::out 文件以输出方式打开
ios::nocreate 不建立文件,所以文件不存在时会打开失败
ios::norplace 不覆盖文件,所以在文件存在时打开失败
ios::trunc 如果文件存在,将文件长度设为0(也就是将文件清空)
一般来说使用或方法将以上多个属性连接起来,如
ios::out|ios::binary
打开文件的属性取值一般有
0:普通文件,打开访问
1:只读文件
2:隐含文件
3:系统文件
在文件读写完成之后,必须将文件关闭,以使文件重新变为可访问的。关闭文件需要调用成员函数close,负责将缓存中的数据释放出来并关闭文件。一旦close函数被调用,原先的流对象就可以被用来打开其他的文件了,这个文件也就可以重新被其他的进程访问。
为防止流对象被销毁时还联系着打开的文件,析构函数将会自动调用close函数
文本文件的处理
文本文件是以ASCII保护文件,可以用字符处理软件来处理。文本文件的读写很简单,用流插入运算符向文件输出,用析取运算符向文件中输入
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream outfile;
ifstream infile;
char value;
outfile.open("a.txt");
outfile << "abcd";
outfile.close();
return 0;
}
这一程序,首先定义了一个ofstream类的变量outfile,建立了一个a.txt文件,通过流插入运算符<<将字符串"abcd"写入该文件中,最后关闭该文件。利用如下程序可以在刚刚的a.txt文件的abcd后面追加一个字符串efg
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream outfile;
ifstream infile;
char value;
outfile.open("a.txt", ios::out | ios::app);
outfile << "efg";
outfile.close();
return 0;
}
运行该程序之后,可以在a.txt文件中看到在原来的字符串abcd之后追加了字符串efg
从文本文件中读入内容
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream outfile;
ifstream infile;
char value;
infile.open("a.txt");
if (infile.is_open())
{
while (infile.get(value))
{
cout << value;
}
}
cout << endl;
infile.close();
return 0;
}
本例中首先定义了一个ifstream类的变量infile,打开文本文件a.txt,通过<<将字符串循环输出到屏幕上,最后关闭该文件