从一个文本文件中读取内容并打印,之后利用sort函数将内容排序后输出到新的文件中

该博客介绍了一个从指定文件中读取内容,将其存储到向量中,然后利用sort函数排序,并将排序后的内容输出到新文件的过程。在读写文件时,注意错误处理,以确保文件能正确打开。排序完成后,还会在新文件中添加当前日期和时间。
摘要由CSDN通过智能技术生成
/* Open a file, copy its content to vector container and print.
   Then sort the contents using 'sort()' and output to another file(date and time included). */
#include <iostream>
#include <string>       // for class string
#include <fstream>      // for file I/O
#include <vector>       // for container vector
#include <algorithm>    // for mthod sort()
using namespace std;

int main()
{
    vector<string> vec;
    string val;
    ifstream infile("C:\\Users\\Desktop\\1.7.txt");  // pay attention to the description of escape sequence
    if(!infile)

        cerr << "Oops! unable to open the file -- 1.7.txt.\n";

    ofstream outfile("C:\\Users\\Desktop\\1.7.1.txt", ios_base::app);    // path can be different

    // 若不加后缀,则每次都会将原先内容覆盖  

     if(!outfile)        //

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值