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

📄 c18_04.cpp

📁 it can help you use C++ program we
💻 CPP
字号:
//对文件的读写操作
#include  <iostream >
#include  <fstream >
#include  <iomanip>
using namespace std;

int main( )
{
	ofstream outfile;
	ifstream infile;
	int	x;					//用来存放读入的整数

	outfile.open("test.txt");
	if( !outfile )			// 判断文件是否成功打开
	{
		cout<<"文件打开失败!"<<endl;
		return 1;
	}
	cout<<"请输入已列数字,以9999结束,如:123 34 9999"<<endl;
	cin>>x;
	while( x != 9999 )
	{
		outfile<<x<<" ";	// 加入一个空格以分隔每个数字
		cin>>x;
	}
	cout<<"数字已经写入文件!\n";
	outfile.close();
//////////////////////////////////////////////////////////////
	infile.open( "test.txt" );		//整数文件
	if( !infile )					//判断是否打开文件成功
	{
		cout << "打不开输入文件 test.txt " << endl; 
		return 1;
	}

	//从文件中读入整数并输出,直至文件尾,循环结束
	while( infile >> x )  
		cout <<setw(8)<< x ;

	cout << "\n读入文件结束!\n" << endl;
	infile.close();					//关闭文件

	return 0;
}

⌨️ 快捷键说明

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