delete a line from a text file

Ancient Dragon 0 Tallied Votes 10K Views Share

This is a simple example of how to delete a line from a text file. In order to do that you have to completely rewrite the file, leaving out the line(s) you want to delete. First open the input file, then open an output file (doesn't matter what filename you give it as long as its a legal file name for your computer system). After read/write loop finishes, close both files, delete the original (or rename it if you don't want to delete it) and rename the new temp file to the original filename.

#include <cstdio>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string line;
    // open input file
    ifstream in("infile.txt");
    if( !in.is_open())
    {
          cout << "Input file failed to open\n";
          return 1;
    }
    // now open temp output file
    ofstream out("outfile.txt");
    // loop to read/write the file.  Note that you need to add code here to check
    // if you want to write the line
    while( getline(in,line) )
    {
        if(line != "I want to delete this line")
            out << line << "\n";
    }
    in.close();
    out.close();    
    // delete the original file
    remove("infile.txt");
    // rename old to new
    rename("outfile.txt","infile.txt");

    // all done!
    return 0;
}
dnalor 0 Newbie Poster

can teach how to save your data in text file using c++??

please help me!!

your code is very good....

aastephen 0 Newbie Poster

hw can in find and delete a line in c++ please give a sample

aastephen 0 Newbie Poster

hi i think this is use full
//-------------------------------------------------------------------------

pragma once
#include <iostream>
#include<conio.h>
#include<fstream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<iomanip>
using namespace std;


// main class


class SuperClass
{
public:
int eid,cid,sid;
char ename[20],cname[20],sname[20];
char status[10];//Admnistrator or user



SuperClass(void);
void addEmployee();
void findEmployee();
void addCustomer();
void findCustomer();
void addSupplier();
void findSupplier();
};


//-------------------------------------------------------------------------
#include "SuperClass.h"


SuperClass::SuperClass()
{
}


void SuperClass::addEmployee()
{
char ch='y';
fstream empFileOpen;
empFileOpen.open("employee.txt",ios::in);
if(!empFileOpen)
{
cerr<<"can't open employee.txt"<<endl;
exit(1);
}
empFileOpen>>eid>>ename>>status;
eid=999;
while(!empFileOpen.eof())
{
empFileOpen>>eid>>ename>>status;
}
eid++;
empFileOpen.close();


fstream empFile;
empFile.open("employee.txt",ios::out|ios::app);
if(!empFile)
{
cerr<<"can't create employee.txt"<<endl;
exit(1);
}
while(ch=='y'||ch=='Y')
{
cout<<"employee name\t\t: ";
cin>>ename;
cout<<"employee status <admin/user>\t\t: ";
cin>>status;
empFile<<eid<<"\t"<<ename<<"\t"<<status<<endl;


cout<<"eid of "<<ename<<" is "<<eid<<endl;
eid++;
cout<<"continue? <Y/N> : ";
cin>>ch;
}
empFile.close();
}
majestic0110 187 Nearly a Posting Virtuoso

Great snippet AD!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.