读入字符存入磁盘文件
1 #include <iostream> 2 #include <fstream> 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 void save_to_file() 6 { 7 ofstream outfile("f2.dat"); 8 if(!outfile) 9 { 10 cerr<<"open f2.dat error!"<<endl; 11 exit(1); 12 } 13 char c[80]; 14 cin.getline(c,80); 15 for(int i=0;c[i]!=0;i++) 16 if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122) 17 { 18 outfile.put(c[i]); 19 cout<<c[i]; 20 } 21 cout<<endl; 22 outfile.close(); 23 } 24 25 void get_from_file() 26 { 27 char ch; 28 ifstream infile("f2.dat",ios::in|ios::nocreate); 29 if(!infile) 30 { 31 cerr<<"open f2.dat error!"<<endl; 32 exit(1); 33 } 34 ofstream outfile("f3.dat"); 35 if(!outfile) 36 { 37 cerr<<"open f3.dat error!"<<endl; 38 exit(1); 39 } 40 while(infile.get(ch)) 41 { 42 if(ch>=97&&ch<=122) 43 ch=ch-32; 44 outfile.put(ch); 45 cout<<ch; 46 } 47 cout<<endl; 48 infile.close(); 49 outfile.close(); 50 } 51 int main(int argc, char** argv) { 52 save_to_file(); 53 get_from_file(); 54 return 0; 55 }