文件的操作:读取某一行开头,删除文件

    /*!
    * @brief      返回文件中第n行,字符串sign之前的字符串
    * @param [in] const std::string * filepath   文件路径
    * @param [in] const size_t        n          第n行
    * @param [in] const std::string   sign       字符串标识
    * @return     bool
    */ 
    std::string GetLineBeginSign(const std::string filepath, const size_t n, const std::string sign);
    /*!
    * @brief      删除文件
    * @param [in] const std::string filepath   文件路径
    * @return     bool
    */ 
    bool DeleteFile(const std::string filepath);

std::string CTestHelper::GetLineBeginSign(const std::string filepath, const size_t n, const std::string sign)
{
    ifstream inFile(filepath);
    std::string line, str;
    if (inFile)
    {
        // 找到第n行
        int i = 0;
        while(i < n)
        {
            if( !getline(inFile, line))
                cout<<"file "<<filepath<<" does not have "<<i + 1<<" lines"<<endl;
            i ++;
        }        

        // 找到sign的位置
        size_t pos = line.find_first_of(sign);
        if(pos == string::npos)
            cout<<"file "<<filepath<<" "<<i + 1<<" line does not have "<<sign<<endl;
        str = line.substr(0, pos);
    }
    return str;
}

bool CTestHelper::DeleteFile(const std::string filepath)
{
    if (-1 == remove(filepath.c_str()))
    {
        return false;
    }

    return true;
}

// 删除文件
    string delFile = (string)"del" + " " + simplydMeshPath;
    system(delFile.data());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值