473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 8722

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
5
by: ad | last post by:
I want to delete some text which begin with <!DOCTYPE and end with > in a file. How can I do it?
6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
6
by: santa19992000 | last post by:
I have a file called xyz.txt, if the first word matches, I have to delete the whole line, how can I do that. sample is below file xyz.txt ======= SP 0/23/2345/345 CL 00/34/3456/54 In the...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
9
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of...
9
by: Juergen Huber | last post by:
hello, i am a dummy user in python and new in this programming language and so there will be questions, that´s for you no problem! i never have before programmed in any language! sorry for this!...
4
by: jeepers | last post by:
Hi all, my problem is delete a line in a text file. the file contain -n line with user, userCode. i want to replace the line with user is egual at user, and/or insert new line but delete the...
10
by: Joah Senegal | last post by:
Hello all, I want something that should be realy really simple.... but I dont get it.. searched google and stuf, but found nothing useful. I;m opening a txt-file with a fstream. Now I want to...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.