#include <iostream> #include <fstream> using namespace std; void main() { ofstream in; in.open("com.txt",ios::trunc); //ios::trunc表示在打開文件前將文件清空,由於是寫入,文件不存在則創建 int i; char a='a'; for(i=1;i<=26;i++)//將26個數字及英文字母寫入文件 { if(i<10) { in<<"0"<<i<<"\t"<<a<<"\n"; a++; } else { in<<i<<"\t"<<a<<"\n"; a++; } } in.close();//關閉文件 } #include <iostream> #include <fstream> using namespace std; void main() { char buffer[256]; fstream out; out.open("com.txt", ios::in); cout << "com.txt" << " 的內容如下:" << endl; while (!out.eof()) { out.getline(buffer, 256, '\n');//getline(char *,int,char) 表示該行字符達到256個或遇到換行就結束 cout << buffer << endl; } out.close(); cin.get();//cin.get() 是用來讀取回車鍵的,如果沒這一行,輸出的結果一閃就消失了 }