加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

利用ifstream和stringstream逐行格式化读取文件

(2012-10-26 16:53:14)
标签:

c

ifstream

stringstream

文件

分词

分类: C/C++技术

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <sstream>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
 std::ifstream ifsAddr("person.txt");

 std::string strTemp;
 std::string strName;
 std::string strSex;
 int nAge = 0;
 std::stringstream strStream;
 while (ifsAddr.good() && !ifsAddr.eof())
 {
  getline(ifsAddr, strTemp, '\n');
  strStream.clear();
  strStream.str(strTemp);

  strStream >> strName>> strSex >> nAge;
  if (strStream.fail())
   continue;

  std::cout << strName << " " << strSex << " "  << nAge << std::endl;
 }

 return 0;
}

输出:

张一 男 19
张二 男 20
张三 男 21

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有