468,512 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

How to delete a line from the file

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!!

Jul 26 '06 #1
1 8367

"ricky" <ri*******@yahoo.co.inwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>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::ostream& 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::ofstream 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
Jul 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by | last post: by
5 posts views Thread by ad | last post: by
6 posts views Thread by I am Sam | last post: by
3 posts views Thread by silver360 | last post: by
9 posts views Thread by groleo | last post: by
9 posts views Thread by Juergen Huber | last post: by
10 posts views Thread by Joah Senegal | last post: by
2 posts views Thread by Francesco Pietra | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.