欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++文件的输入输出

程序员文章站 2024-03-18 18:07:10
...

包含头文件#include<fstream>
C++文件的输入输出
基本步骤如下:

1、建立文件流对象。

ifstream in;	//从硬盘到内存
ofstream out;	//从内存到硬盘
fstream both;

2、使用成员函数open()打开。

文件流对象.open(文件名, 使用方式);
C++文件的输入输出

3、进行读写操作。

在下面举例着重讲解读写操作。

4、使用成员函数close()将打开的文件关闭。

文件流对象.close();

【文本文件的读写】

文本文件的读取和写入都十分容易,流类库的输入输出操作<<, put, write, >>, get, getline等都可以用于文本文件的输入输出。

#include<iostream>
#include<fstream> 
using namespace std;

int main()
{
	//从内存到硬盘 
	ofstream fout("test.txt", ios::out);	//调用构造函数打开文件
	if(!fout)
	{
		cout<<"Cannot open output file.\n";
		exit(1);
	}
	fout<<"I am a student.";	//输出到磁盘文件
	fout.close();
	return 0;
}

运行结果如下:
C++文件的输入输出

#include<iostream>
#include<fstream> 
using namespace std;

int main()
{
	ifstream fin("test.txt", ios::in);	//从硬盘到内存
	if(!fin)
	{
		cout<<"Cannot open input file.\n";
		exit(1);
	}
	char str[80];
	fin.getline(str, 80);	// 从磁盘文件输入
	cout<<str<<endl;
	fin.close();
	return 0;
}

运行结果如下:
C++文件的输入输出

【二进制文件的读写】

1、用get()和put()函数(一个字节)

#include<iostream>
#include<fstream> 
using namespace std;

int test_put()
{
	//从内存到硬盘 
	ofstream fout("f3.bin", ios::binary);	//调用构造函数打开文件
	if(!fout)
	{
		cout<<"Cannot open output file.\n";
		exit(1);
	}
	char ch = 'a';
	for(int i = 0; i < 26; i++)
	{
		fout.put(ch);	//输出到磁盘文件
		ch++;
	}	
	fout.close();
	return 0;
}

int test_get()
{
	ifstream fin("f3.bin", ios::binary);	//从硬盘到内存
	if(!fin)
	{
		cout<<"Cannot open input file.\n";
		exit(1);
	}
	char ch;
	while(fin.get(ch))	//从磁盘文件输入
		cout<<char(ch-32);
	fin.close();
	return 0;
}

int main()
{
	test_put();
	test_get();
	return 0;
}

运行结果如下:
C++文件的输入输出
C++文件的输入输出

2、用read()和write()函数(一组数据)

#include<iostream>
#include<fstream> 
using namespace std;

struct list
{
	char course[15];
	int score;
};

int main()
{
	list L1[2] = {"computer", 90, "math", 99};
	list L2[2];
	//从内存到硬盘 
	ofstream fout("f3.bin", ios::binary);	//调用构造函数打开文件
	if(!fout)
	{
		cout<<"Cannot open output file.\n";
		abort();	//退出程序 
	}
	for(int i = 0; i < 2; i++)
		fout.write((char*) &L1[i], sizeof(L1[i]));	//输出到磁盘文件
	fout.close();
	
	//从硬盘到内存
	ifstream fin("f3.bin", ios::binary);
	if(!fin)
	{
		cout<<"Cannot open input file.\n";
		abort();
	}
	for(int i = 0; i < 2; i++)
	{
		fin.read((char*) &L2[i], sizeof(L2[i]));	//从磁盘文件输入
		cout<<L2[i].course<<" "<<L2[i].score<<endl;
	}
	fin.close();
	return 0;
}

运行结果如下:
C++文件的输入输出
C++文件的输入输出

【二进制文件的随机读写】

C++文件的输入输出

其中的参数如下:

  • “文件中的位置”:long型整数,以字节为单位
  • “位移量”:long型整数,以字节为单位
  • “参照位置”:
    ios::beg从文件头计算要移动的字节数
    ios::cur从文件指针的当前位置计算要移动的字节数
    ios::end从文件末尾计算要移动的字节数

C++文件的输入输出

#include<iostream>
#include<fstream> 
using namespace std;

struct list
{
	char course[15];
	int score;
};

int main()
{
	list L1[3] = {{"computer", 90}, {"math", 99}, {"english", 84}};
	list L2;
	//从内存到硬盘
	fstream ff("f3.bin", ios::out|ios::binary);	 //调用构造函数打开文件
	if(!ff)
	{
		cout<<"Cannot open output file.\n";
		abort();	//退出程序 
	}
	for(int i = 0; i < 3; i++)
		ff.write((char*) &L1[i], sizeof(list));	//输出到磁盘文件
	ff.close();
	
	//从硬盘到内存
	fstream ff1("f3.bin", ios::in|ios::binary);
	if(!ff1)
	{
		cout<<"Cannot open input file.\n";
		abort();
	}
	
	ff1.seekp(sizeof(list)*2);	//将文件指针定位到第3门课程
	ff1.read((char*) &L2, sizeof(list));	//读取第3门课程的数据
	cout<<L2.course<<" "<<L2.score<<endl;	//显示第3门课程的数据
	
	ff1.seekp(sizeof(list)*0);	//将文件指针定位到第1门课程
	ff1.read((char*) &L2, sizeof(list));	//读取第1门课程的数据
	cout<<L2.course<<" "<<L2.score<<endl;	//显示第1门课程的数据
	
	ff1.seekp(sizeof(list)*1, ios::cur);		//将文件指针从当前位置移动一门课程 
	ff1.read((char*) &L2, sizeof(list));		//读取该门课程的数据
	cout<<L2.course<<" "<<L2.score<<endl;	//显示该门课程的数据
	ff1.close();
	return 0;
}

运行结果如下:
C++文件的输入输出
C++文件的输入输出

相关标签: C/C++