Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505517
  • 博文数量: 111
  • 博客积分: 3160
  • 博客等级: 中校
  • 技术积分: 1982
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-24 11:49
个人简介

低调、勤奋。

文章分类

全部博文(111)

文章存档

2014年(2)

2013年(26)

2012年(38)

2011年(18)

2010年(27)

分类: C/C++

2012-12-22 11:48:04

一、IO标准库类型和头文件
头文件:
iostream,读写流,如cout cin。
fstream:读写文件,ifstream:从文件中读取,ofstream,写到文件中去。

二、IO对象不可复制或赋值。


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <cstring>

  6. using namespace std;

  7. vector<string> names;
  8. vector<string> phones;

  9. void process(string);

  10. int main()
  11. {
  12.     vector<string> files;
  13.     string fileName, s;

  14.     cout << "Enter [i] enames: (Ctrl + Z to end)" << endl;

  15.     while (cin >> fileName && strcmp(fileName.c_str(),"stop"))
  16.     {
  17.         files.push_back(fileName);
  18.     }
  19.     ifstream input;
  20.     ofstream file2;
  21.     file2.open("result");

  22.     vector<string>::const_iterator it = files.begin();

  23.     while (it != files.end())
  24.     {
  25.         input.open(it->c_str());
  26.         if (!input)
  27.         {
  28.             cerr << "error can not open the file " << *it << endl;
  29.             input.clear(); //清除文件流的状态
  30.             ++it;
  31.         }
  32.         else
  33.         {
  34.             while (input >> s)
  35.             {
  36.                 process(s);
  37.             }
  38.             input.close();
  39.             input.clear();
  40.             ++it;

  41.         }
  42.     }

  43.     cout << "complete " << "will output all names and phones " << endl;
  44.     int name_nus = 0;
  45.     int phone_nus = 0;
  46.     vector<string>::const_iterator phone = phones.begin();
  47.     vector<string>::const_iterator name = names.begin();

  48.     while (name != names.end() && phone != phones.end())
  49.     {
  50.         cout << "names=" << *name << "phones=" << *phone << endl;
  51.         
  52.         if (file2.good())
  53.          file2 << *name << "," << *phone << endl;

  54.         name++;
  55.         name_nus++;
  56.         phone++;
  57.         phone_nus++;
  58.     
  59.     }
  60.     if (file2)
  61.     {
  62.         file2.close();
  63.     }

  64.     cout << "finaly phones nus = " << phone_nus << "names nums = " << name_nus << endl;
  65.     
  66.     return 0;

  67. }

  68. void process(string s)
  69. {
  70.     string::size_type position;
  71.     position = s.find("FN:");
  72.      if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示
  73.      {
  74.       position = s.find(":");
  75.         position++;
  76.           cout << "position is : " << position << "name=" << s.substr(position) << endl;
  77.         names.push_back(s.substr(position));
  78.      }
  79.     else
  80.      { //find phone number
  81.         position = s.find("CELL:");
  82.         if (position != s.npos)
  83.         {
  84.             position = s.find(":");
  85.             position++;
  86.              cout << "position is : " << position << "phone=" << s.substr(position) << endl;
  87.             phones.push_back(s.substr(position));
  88.         }
  89.         else
  90.         {
  91.               cout << "Not found" << endl;
  92.         }    
  93.     }    
  94. }


阅读(597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~