?? c18_04.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 + -