对ASCII文件的操作
生活随笔
收集整理的這篇文章主要介紹了
对ASCII文件的操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ASCII文件又稱文本(text)文件或字符文件,它的每一個字節放一個ASCII代碼,代表一個字符
對ASCII文件的讀寫操作可以用以下兩種方法:
(1)用流插入運算符“<<”和流提取運算符“>>”輸入輸出標準類型的數據。?
“<<”和“>>”都已在iostream中被重載為能用于ostream和istream類對象的標準類型的輸入輸出。由于ifstream和ofstream分別是ostream和istream類的派生類,因此它們從ostream和istream類繼承了公用的重載函數,所以在對磁盤文件流對象和流對象運算符?“<<”和流提取運算符“>>”實現對磁盤文件讀寫,如同用cin,cout和<<,>>對標準設備進行讀寫一樣。
(2)用文件流的put,get,getline等成員函數進行字符的輸入輸出
//有一個整型數組,含10個元素,從鍵盤輸入10個整數給數組,將此數組送到磁盤文件中存放 #include<fstream> #include<iostream> using namespace std; int main() {int a[10];ofstream outfile("f1.dat",ios::out);if(!outfile){ cerr<<"open error!"<<endl;exit(1);}cout<<"enter 10 integer numbers:"<<endl;for(int i=0;i<10;i++){ cin>>a[i];outfile<<a[i]<<" ";}outfile.close();return 0; }
//從上面建立的數據文件f1.dat中讀入10個整數放在數組中,找出并輸出10個數中的最大者和它在數組中的序號 #include<iostream> #include<fstream> using namespace std; int main() {int a[10],max,i,order;ifstream infile("f1.dat",ios::in);if(!infile){ cerr<<"open error!"<<endl;exit(1);}for(i=0;i<10;i++){ infile>>a[i];cout<<a[i]<<" ";}cout<<endl;max=a[0];order=0;for(i=1;i<10;i++)if(a[i]>max){ max=a[i];order=i;}cout<<"max="<<max<<endl<<"order="<<order<<endl;infile.close();system("pause");return 0; }
轉載于:https://www.cnblogs.com/wwj9413/archive/2011/11/26/2781280.html
總結
以上是生活随笔為你收集整理的对ASCII文件的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android_2.2_eclips_B
- 下一篇: 扩展Jquery插件处理mouseove