debugk|C++

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 1 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

最新随笔

搜索

  •  

积分与排名

  • 积分 - 12
  • 排名 - 3775

最新随笔

最新评论

 

 1// append.cpp -- appending information to a file
 2#include <iostream>
 3#include <fstream>
 4#include <string>
 5#include <cstdlib>      // (or stdlib.h) for exit()
 6
 7const char * file = "guests.txt";
 8int main()
 9{
10    using namespace std;
11    char ch;
12
13// show initial contents
14    ifstream fin;
15    fin.open(file);
16
17    if (fin.is_open())
18    {
19        cout << "Here are the current contents of the "
20             << file << " file:\n";
21        while (fin.get(ch))
22            cout << ch;
23        fin.close();
24    }

25
26// add new names
27    ofstream fout(file, ios::out | ios::app);
28    if (!fout.is_open())
29    {
30        cerr << "Can't open " << file << " file for output.\n";
31        exit(EXIT_FAILURE);
32    }

33
34    cout << "Enter guest names (enter a blank line to quit):\n";
35    string name;
36    while (getline(cin,name) && name.size() > 0)
37    {
38          fout << name << endl;
39    }

40    fout.close();
41
42// show revised file
43    fin.clear();    // not necessary for some compilers
44    fin.open(file);
45    if (fin.is_open())
46    {
47        cout << "Here are the new contents of the "
48             << file << " file:\n";
49        while (fin.get(ch))
50            cout << ch;
51        fin.close();
52   }

53    cout << "Done.\n";
54    return 0
55}

56
posted on 2009-03-21 19:54 debugk 阅读(4) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理