Am trying to edit and also delete data in my text file but am finding a little problem...i want to read the specific text and the program stops when it reaches an end of line and not eof..i tried using '\n' but maybe I used it wrong cuz it cant seem to work...lets say the file contains the following data

hello 33
welcome 89
thanks 56

i want to read "welcome 89" and change it to maybe "this 77"..also i want to delete something in the file say "thanks 35"...any help is appreciated

Recommended Answers

All 8 Replies

Open the input file for reading
Open an output file for writing while ( fin.getline(myLine) ) { } Close input file
Close output file


Now in the while loop you
- write the line unchanged
- write a different line
- don't write the line at all.

i have managed to get the details line by line but how do i play around with the data i.e delete info, edit info...Basically i want the user to enter a name in the list, then if the name is found i can perform my operation...here is what i managed with getting the string from the user..

ifstream fin("name.txt");
    string s;
    cin >> name_in;
	
	while( getline(fin,s) ) 
	{
		if (s==name_in)
		{
		 cout << "Read from file: " << s<< endl; //trying to test but it cannot work..
		}
	}

Read the help for std::string to find all the methods it supports for extracting bits of information.

i read through the std::string and i really got alot from there but i have still failed to solve my tiny problem...here is the more precise problem...if my text file has the following;

hello 33
people 45
okay 65

if the user inputs hello, the whole string of "hello 33" is deleted from the text file....i have created a temp file to copy everything apart from "hello 33" but i dont know where to start..i have tried comparing strings but i get stuck on how the loop should ignore the string "hello 33"...thanx for any help

has anyone got an idea on my problem yet??

but i dont know where to start..i have tried comparing strings but i get stuck on how the loop should ignore the string "hello 33"

Ignoring the string is the same as not writing it to the output file at all.

Like Salem stated:

Now in the while loop you
- write the line unchanged
- write a different line
- don't write the line at all.

well the question is how do i get to read in the first line and not read the second one...that is what is puzzling me...cuz i got an idea of writing the lines that are not ignored to a temp file and then replace the original line..could you like gimmie a line of code that can do that or explain what comand i can use to do that

well the question is how do i get to read in the first line and not read the second one...that is what is puzzling me...cuz i got an idea of writing the lines that are not ignored to a temp file and then replace the original line..could you like gimmie a line of code that can do that or explain what comand i can use to do that

I think what they are saying is to treat read and write as two completely independent operations. You read in all of the lines. For each line that is read in, based on some criteria, either write that line to the output file, write something different into the output file, or write nothing to the output file. To "delete", you write nothing to the output file. So you "ignore" nothing when you read it in, regardless of what you do with it. Regardless of what you do with the lines once you have read them, you read in all of them, so the goal you are trying to achieve:

how do i get to read in the first line and not read the second one...

is not what you should be trying to do.

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.