• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
fstream--文本数据迁移
 1 // ifile_ofile_test.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 
 9 #include <iostream>
10 #include <fstream>
11 #include <string>
12 
13 int move_data(const std::string _ifilename, const std::string _ofilename)
14 {
15     char cVal[8] = {0};
16     std::string line;
17     try
18     {
19         std::ifstream ifile(_ifilename.c_str(),std::ios::in);//只读方式读取文件
20         std::ofstream ofile(_ofilename.c_str(), std::ios::out|std::ios::app);//输出到新文件中
21 
22         if (ifile.fail())
23         {
24             std::cout<<"Unable to init ifstream file"<<std::endl;
25         }
26         if (ofile.fail())
27         {
28             std::cout<<"Unable to init ofstream file"<<std::endl;
29         }
30         else if (ifile.is_open()&&ofile.is_open())
31         {
32             /*读数据*/
33             while(getline(ifile, line))
34             {
35                 ofile << line<<std::endl;
36             }
37             ifile.close();
38             ofile.close();
39             return 0;
40         }
41         else
42         {
43             std::cout<<"error"<<std::endl;
44         }
45     }
46     catch (std::exception& exc)
47     {
48         std::cout<<"error:"<<exc.what()<<std::endl;
49     }
50 
51     return 0;
52 }
53 int _tmain(int argc, _TCHAR* argv[])
54 {
55     std::string ifilename="http://www.cnblogs.com/infile.txt";
56     std::string ofilename="http://www.cnblogs.com/outfile.txt";
57     if(move_data(ifilename,ofilename)==0)
58     {
59         std::cout<<"迁移数据成功"<<std::endl;
60     }
61     return 0;
62 }

 

posted on 2012-12-27 17:05  lovemu  阅读(376)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3