How to delete a line from the file

R

ricky

I am a student doing internship. Iam doing a project.
I need to remove or update a line in the file.
So far
1.I can read from file
2. I can write to the file
3. I can find the string in the file i want to delete

Now,I want to take the input from one file and write all the infomation
to the new file except the one i don't want.
I can take the infrmation frm one file and write it to the new file and
delete the old file.

The only and imp bit i can't figure out is how to write all the
information except the one i dnt want
Would be something like

strncmp()
if(found)
here i want to write something that if found dnt write to new file
else
file<<all the lines;

Please help!!
 
M

Mike Wahler

ricky said:
I am a student doing internship. Iam doing a project.
I need to remove or update a line in the file.
So far
1.I can read from file
2. I can write to the file
3. I can find the string in the file i want to delete

Now,I want to take the input from one file and write all the infomation
to the new file except the one i don't want.
I can take the infrmation frm one file and write it to the new file and
delete the old file.

The only and imp bit i can't figure out is how to write all the
information except the one i dnt want
Would be something like

strncmp()
if(found)
here i want to write something that if found dnt write to new file
else
file<<all the lines;

Please help!!

#include <fstream>
#include <istream>
#include <iostream>
#include <ostream>
#include <string>

void omit(std::istream& in, std::eek:stream& out, const std::string s)
{
static std::string line;
while(std::getline(in, line))
if(line != s)
out << line << '\n';
}

int main()
{
const std::string in_name("input.txt");
const std::string out_name("output.txt");

std::ifstream input(in_name.c_str());
std::eek:fstream output(out_name.c_str());

if(!input)
std::cerr << "Cannot open input\n";

if(!output)
std::cerr << "Cannot open output\n";

if(input && output)
omit(input, output, "remove me");

if(!input.eof())
std::cerr << "Error reading input\n";

if(!output)
std::cerr << "Error writing output\n";

return 0;
}


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top