⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11_25.cpp

📁 C++案例教程源代码
💻 CPP
字号:
#include <iostream> 
#include <fstream> 
using namespace std; 
int main() 
{ 	int n[5] = {1, 2, 3, 4, 5}; 	register int i; 
	ofstream ofile("test.bin", ios::out | ios::binary); 	//以二进制方式打开文件
	if(!ofile) 	{ 	cout << "Cannot open file.\n"; 	return 1; 	} 
	ofile.write((char *) &n, sizeof n); //向文件写数据块
	ofile.close(); 
	for(i=0; i<5; i++) 		n[i] = 0; 
	ifstream ifile("test.bin", ios::in | ios::binary); 	//以二进制方式打开文件
	if(!ifile) 	{ 	cout << "Cannot open file.\n"; 		return 1; 	} 
	ifile.read((char *) &n, sizeof n); 
	for(i=0; i<5; i++) 		cout << n[i] << " "; 	//从文件中读数据
	ifile.close(); 
	return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -