码迷,mamicode.com
首页 > 其他好文 > 详细

读入字符存入磁盘文件

时间:2018-08-02 11:18:59      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:cout   div   loop   use   error   span   file   rom   ram   

 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 }

 

读入字符存入磁盘文件

标签:cout   div   loop   use   error   span   file   rom   ram   

原文地址:https://www.cnblogs.com/borter/p/9405574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!