当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

C++中删除文本的最后一行

时间:2014/11/16 作者:平凡之路 来源:xuhantao.com 浏览:

读取文本的每行("\n"), 存储入数组vector<string>, 输出时, 少输出最后一行, 即可.

代码:

/* 
 * main.cpp 
 * 
 *  Created on: 2014.06.08 
 *      Author: Spike 
 */
      
/*vs 2012*/
      
#include <windows.h>  
#include <fstream>  
#include <iostream>  
#include <string>  
#include <vector>  
      
using namespace std;  
      
int main()  
{  
    vector<string> tmp_files;  
      
    ifstream infile( "w.txt" );  
    if (!infile) {  
        cout << "fail!" << endl;  
        return 0;  
    }  
      
    string lineContent;  
    while ( getline( infile, lineContent, '\n' ) ){  
        tmp_files.push_back(lineContent + "\n" );  
    }  
    infile.close();  
      
    ofstream outfile( "w2.txt",ios::out );  
    vector<string>::iterator siter = tmp_files.begin();  
      
    copy( tmp_files.begin(), tmp_files.end()-1, ostream_iterator<string>(outfile) );  
    cout << "ok!" << endl;  
    outfile.close();  
      
    return 0;  
}

输出:

原始文本(w.txt):  
Female  
Sister  
Girl  
Woman  
Old Woman  
      
输出文本(w2.txt):  
Female  
Sister  
Girl  
Woman

作者:csdn博客 Spike_King

更多精彩内容:http://www.bianceng.cn/Programming/cplus/

相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1