zoukankan      html  css  js  c++  java
  • 读入字符存入磁盘文件

     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 }
  • 相关阅读:
    LeetCode Lect7 堆及其应用
    Leetcode Lect7 哈希表
    5105 pa3 Distributed File System based on Quorum Protocol
    5105 pa2 Distributed Hash Table based on Chord
    5105 pa1 MapReduce
    分布式系统知识总结
    OS知识点总结
    c++知识点总结3
    c知识点总结2
    c++知识点总结
  • 原文地址:https://www.cnblogs.com/borter/p/9405574.html
Copyright © 2011-2022 走看看