/*----------------------------------字符文件操作例子------------------------------*/ /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std;
void main()//向字符文件輸出數(shù)據(jù) { ofstream f1("d:\\VC2008\\wr1.dat");//定義輸出文件流并打開,。 if(!f1) { cerr<<"wr1.dat not open!"<<endl; exit(1); } for(int i=0;i<20;i++) f1<<i<<" "; f1.close();//關(guān)閉f1對應(yīng)的文件 } */ //從鍵盤輸入文本字符到wr2.dat,,直到從鍵盤按下ctrl+c(代表文件結(jié)束符EOF)為止 /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std; void main()//向字符文件輸出數(shù)據(jù) { char ch; ofstream f2("D:\\VC2008\\wr2.dat"); if(!f2) { cerr<<"file wr2.dat not open!"<<endl; exit(1); } cout<<"input string:(ctrl+c end)"<<endl; ch = cin.get();//從cin流中提取一個字符到ch中 while(ch!=EOF) { f2.put(ch);//把ch字符寫入到f2流中,,此語句也可以用 f2<<ch代替 ch = cin.get();//繼續(xù)從cin流提取下個字符到ch中 } f2.close();//關(guān)閉f2所對應(yīng)的文件 }*/ /*-------------------------------------------------------------------------------------*/ /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std;
void main()//從字符文件輸入數(shù)據(jù) { ifstream f1("d:\\VC2008\\wr1.dat");//定義輸入文件流,,并打開相應(yīng)文件 if(!f1) { cerr<<"file not open!"<<endl; exit(1); } int x; while(f1>>x)//依次從文件輸入整數(shù)到x(自動跳過空白字符),,當(dāng)讀到文件結(jié)束符時,表達(dá)式值為0 { cout<<x<<' '; } cout<<endl; f1.close(); }*/ /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std;
void main() { ifstream f2("d:\\VC2008\\wr2.dat",ios::in);//ios::nocreate(vs2008無) ifstream中的open函數(shù)默認(rèn)打開就是不創(chuàng)建文件 if(!f2) { cerr<<"file not open!"<<endl; exit(1); } char ch;//用ch讀入字符 int i=0; while(f2.get(ch)) { cout<<ch; if(ch=='\n') i++; } cout<<endl<<"lines:"<<i<<endl; f2.close(); }*/ //從鍵盤輸入若干行字符到一個二維字符數(shù)組a,,然后分別統(tǒng)計出a中保存的字母,、數(shù)字和其他字符的個數(shù) /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std;
void main() { int n; cout<<"待輸入文本行數(shù):"; cin>>n>>ws;//操縱符ws讀取輸入n值后的換行符 char (*a)[80] = new char [n][80]; int i; for(i=0;i<n;i++) cin.getline(a[i],80); int c1,c2,c3; c1=c2=c3=0; for(i=0;i<n;i++) { char *p =a[i]; char ch = *p; while(ch) { if(ch>=65 && ch<=90 || ch>=97 && ch<=122) c1++;//統(tǒng)計字母個數(shù) else if(ch>=48 && ch<=57) c2++;//統(tǒng)計數(shù)字個數(shù) else c3++;//統(tǒng)計其他字符個數(shù) ch = *++p;//得到該行的下一個字符 } } cout<<endl; cout<<"c1="<<c1<<",c2="<<c2<<",c3="<<c3<<endl; }*/
/*----------------------------------字節(jié)文件操作例子------------------------------*/ /*#include<iostream> #include<stdlib.h> #include<fstream> using namespace std;
void main()//寫入(輸出)到字節(jié)文件 { ofstream f1("d:\\VC2008\\shf1.dat",ios::out|ios::binary);//定義輸出文件流,并打開相應(yīng)的字節(jié)文件 if(!f1) { cerr<<"file not open!"<<endl; exit(1); } int a[10]={11,23,44,55,66,7,2,33,44,52}; for(int i=0;i<10;i++) f1.write((char *)&a[i],sizeof(int)); f1.close(); }*/ //對上面shf1.dat文件中保存的數(shù)求出最大值,、最小值和平均值 /*#include<stdlib.h> #include<fstream> #include<iostream> using namespace std;
void main()//把字節(jié)文件的內(nèi)容輸入到內(nèi)存 { ifstream f1("d:\\VC2008\\shf1.dat",ios::in|ios::binary);//定義輸入文件流 if(!f1) { cerr<<"file not open!"<<endl; exit(1); } int x,max,min; float mean; f1.seekg(0,ios::end);//將文件指針移到文件尾,,此時文件指針位置就是按字節(jié)計算的文件長度 int n=f1.tellg()/sizeof(int);//n為按整型大小計算的文件長度 if(n==0) { cerr<<"輸入文件為空!"<<endl; exit(1); } f1.seekg(0);//將文件指針移到文件開始位置 f1.read((char *)&x,sizeof(int));//讀第一個整數(shù) max=min=x; mean=(float)x/n; for(int i=1;i<n;i++) { f1.read((char *)&x,sizeof(int)); if(x>max) max = x; else if(x<min) min = x; mean += (float)x/n; } cout<<"maximum:"<<max<<endl; cout<<"minimum:"<<min<<endl; cout<<"mean:"<<mean<<endl; f1.close(); }*/ //從一個字符串流中輸入用逗號分開的每個整數(shù)并顯示出來 /*#include<iostream> #include <strstream> #include<stdlib.h> using namespace std; void main() { char a[]="23,2,23 ,34,11,54,23,55 @"; cout<<a<<endl; istrstream sin(a);//定義一個輸入字符串流sin,使用的字符數(shù)組為a char ch=' '; int x; while(ch!='@')//@字符作為字符串流結(jié)束標(biāo)志 { sin>>ws>>x>>ws;//從流中讀入一個整數(shù),,并使用操作符ws讀取一個整數(shù)前后的空白字符 cout<<x<<' '; sin.get(ch);//從sin流中讀入一個字符,,實際讀取的是','或'@'字符 } cout<<endl; }*/ //從一個字符串中得到每個整數(shù),并把它們依次存入到一個字符串流中,,最后向屏幕輸出這個字符串流 /* a;sdkfas;d 2 2234 44 55 3 @ 2 2234 44 55 3 @ 燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙燙蘟;sdkfas;d 2 2234 44 55 3 @ */ #include<strstream> #include<iostream> using namespace std;
void main() { char a[50]; char b[50]; istrstream sin(a);//定義一個輸入字符串流sin,,使用的字符數(shù)組為a ostrstream sout(b,sizeof(b));//定義一個輸出字符串流sout,使用的字符數(shù)組為b
cout<<"input string+numbers:";
cin.getline(a,sizeof(a)); char ch=' '; int x; while(ch!='@')//使用@作為結(jié)束標(biāo)志 { if(ch >= 48 && ch<=57)//是0-9的數(shù)字 { sin.putback(ch);//把剛讀入的一個數(shù)字壓回流中 sin>>x;//從流中讀入一個整數(shù),,當(dāng)碰到非數(shù)字字符時,,認(rèn)為一個整數(shù)結(jié)束 sout<<x<<' ';//將x輸出到字符串流sout中 } sin.get(ch);//從sin流中讀入下一個字符 } sout<<'@'<<endl; cout<<b<<endl;//輸出字符串流sout對應(yīng)的字符串 }
|