c++ 写进文件并读出

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main() {
  std::vector<std::string> v_ip;
  v_ip.clear();

  v_ip.push_back("192.168.127.11");
  v_ip.push_back("192.168.127.23");
  v_ip.push_back("192.168.127.25");
  v_ip.push_back("192.168.127.45");

  for(typename::std::vector<std::string>::const_iterator it=v_ip.begin(); it!=v_ip.end(); ++it) {
    std::fstream fwrite("/root/filejiaa", std::ios::out); /*open for write*/
    fwrite<<*it<<"\n";
    fwrite.close();
    fwrite.open("/root/filejiaa", std::ios::in); /*open for read*/
    while(!fwrite.eof()) {
      std::string line;
      getline(fwrite, line);
      if(line.empty())
        break;
      std::cout<<line<<"\n";
    }
    fwrite.close();
  }

  return 0;
}

vector里的ip每次读进一个filejiaa文件里,然后再把单个读出。

posted @ 2018-08-16 20:43  东宫得臣  阅读(170)  评论(0编辑  收藏  举报