C读写文件插入删除指定行,C++读写文件插入删除指定行

这段代码展示了如何使用C++和C语言来实现文件中的特定行删除及追加功能。C++代码通过`ifstream`和`ofstream`删除指定行,而C语言代码则使用`fopen`, `fgets`和`fputs`完成相同操作。两段代码都首先读取文件,然后根据条件决定是否删除或追加行,最后将处理后的数据写回文件。
摘要由CSDN通过智能技术生成
C++代码:
#include <string>
#include <fstream>
#include <iostream>

bool fileDelete(std::string path,std::string str1)
{
    //ect/profile文件去掉ulimit配置
    std::ifstream in(path.c_str());
    if(!in.is_open())
    {
        return false;
    }
    std::string strFileData = "";
    std::string lineStr = "";
    while(std::getline(in,lineStr))
    {
        //std::cout << lineStr<<std::endl;
        if (lineStr != str1)
        {
            strFileData += lineStr;
            strFileData += "\n";
        }
    }
    in.close();
    //写入文件
    if(!strFileData.empty())
    {
        std::cout << strFileData<<std::endl;
        std::ofstream out(path.c_str());
        out.is_open();
        out.flush();
        out<<strFileData;
        out.close();
    }
    else
    {
        return false;
    }
    return true;
}

bool fileAppend(std::string path,std::string str1)
{
    bool flag = false;
    std::ifstream in(path.c_str());
    if(!in.is_open())
    {
        return false;
    }
    std::string strFileData = "";
    std::string lineStr = "";
    while(std::getline(in,lineStr))
    {
        //std::cout << lineStr<<std::endl;
        if(lineStr == str1)
            flag = true;
        strFileData += lineStr;
        strFileData += "\n";
    }
    if(!flag)
    {
        strFileData += str1;
        strFileData += "\n";
    }
    in.close();

    //写入文件
    if(!strFileData.empty())
    {
        std::cout << strFileData<<std::endl;
        std::ofstream out(path.c_str());
        out.is_open();
        out.flush();
        out<<strFileData;
        out.close();
    }
    else
    {
        return false;
    }
    return true;
}

c语言代码:

int fileDelete(char* FileName,char str[256])
{
    int   Lid=0;
    int   MaxLine=0;
    FILE*   fp=NULL;
    char   Buf[256]="";
    char   tmp[100][256]={0};

    if ((fp = fopen(FileName, "r+")) == NULL)
    {
        printf("Can't   open   file!/n");
        return 0;
    }

    while (fgets(Buf, 256, fp))
    {
        Lid++;
        if (strcmp(Buf,str) != 0)
        {
            strcpy(tmp[Lid], Buf);
        }
    }

    MaxLine = Lid;
    rewind(fp);
    fclose(fp);
    //remove(FileName);   //   删除原文件
    if((fp = fopen(FileName, "w")) == NULL)
    {
        printf("@@@@@@@@Can't   open   etc/profile!/n");
        return 0;
    }

    for(Lid = 1; Lid <= MaxLine; Lid++)
    {
        fputs(tmp[Lid], fp);
    }
    fclose(fp);
    return 1;
}

int fileAppend(char* FileName, char str[256])
{
    int  flag = -1;
    int   Lid=0;
    int   MaxLine=0;
    FILE*   fp=NULL;
    char   Buf[256]="";
    char   tmp[50][256]={0};


    if ((fp=fopen(FileName,"r+")) == NULL)
    {
        printf("@@@@@@@@@Can't   open   etc/profile!/n");
        return 0;
    }
    while (fgets(Buf, 256 ,fp))
    {
        Lid++;
        if(strcmp(Buf,str) == 0)
        {
            flag = 0;
        }
        strcpy(tmp[Lid],Buf);
    }

    if(-1 == flag)
        strcpy(tmp[Lid++],str);

    MaxLine=Lid;
    rewind(fp);
    for(Lid=1 ;Lid <= MaxLine;Lid++)
    {
        fputs(tmp[Lid],fp);
    }

    fclose(fp);
    return 1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yvonne0627

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值