Deleting data from a txt file

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3972 Views - Last Post: 04 June 2009 - 08:24 PM Rate Topic: -----

#1 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Deleting data from a txt file

Posted 04 June 2009 - 03:13 AM

Okay so I managed to figure out how to search a txt file for a string

the txt file contains data that is set out like so

name#address#phone#date#
name2#address#phone#date#
name3#address#phone#date#

etc...

I'm writing a function that not only searches for name, but deletes that line
I'm new to c++ so here is what i've got so far, i get an error, "no match for operator<<..." at the end of the code

void deleteEntry()
{

	//Step through copy line by line
	//Search the line for the entered search word
	//for the first match, delete the line

	char delSearch[99];
	int i = 0;
	char line[500];


	cout <<"Enter a date you wish to search by: ";
	cin.getline(delSearch, 99);

	ifstream copy ("copy.txt", ios::in);

	while (!copy.eof())  //while copy is not at the end of the file
	{
		copy.getline(line,sizeof(line)); //Get a line from copy and put it into the line array

		if(strstr(line, delSearch) != NULL) //Compare
		{
			for(i = 0; i < sizeof(line); i++) //Turns the line into \0 charachters
			{
					line[i] = '\0';
			}
			copy << line; //This is the bit that is causing problems, how do i remove the line in the txt file with a non existant line?
		}
	}




	return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Deleting data from a txt file

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:18 AM

You are using "ifstream" which allows you to "read" from the file.

If you want to "write" to the file you will have to use a stream that allows writing.

Read more here
http://www.cplusplus...tutorial/files/
Was This Post Helpful? 0
  • +
  • -

#3 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:27 AM

View Postjanotte, on 4 Jun, 2009 - 02:18 AM, said:

You are using "ifstream" which allows you to "read" from the file.

If you want to "write" to the file you will have to use a stream that allows writing.

Read more here
http://www.cplusplus...tutorial/files/


Thanks, Is there anything that allows me to use both.

If i were to put ofstream in the while loop, and write, would it write over the current line?
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:32 AM

View Postl0ckz0r, on 4 Jun, 2009 - 02:27 AM, said:

Thanks, Is there anything that allows me to use both.


Did you read the info at the link?
From the top of the page
"fstream: Stream class to both read and write from/to files."
Was This Post Helpful? 0
  • +
  • -

#5 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:41 AM

yeah sorry I got that, I just wasn't sure if i could do it that way

so now i have


void deleteEntry()
{

	//Step through bookcopy line by line
	//Search the line for the entered search word
	//for the first match, delete the line

	char delSearch[99];
	int i = 0;
	char line[500];


	cout <<"Enter the name you with to delete: ";
	cin.getline(delSearch, 99);

	fstream bookcopy ("bookcopy.txt", ios::in);

	while (!bookcopy.eof())  //while bookcopy is not at the end of the file
	{
		bookcopy.getline(line,sizeof(line)); //Get a line from bookcopy and put it into the line array

		if(strstr(line, delSearch) != NULL) //Compare
		{
			for(i = 0; i < sizeof(line); i++) //Turns the line into \0 charachters
			{
					line[i] = '\0';
			}
			bookcopy << line; 
		}
	}




	return;
}




I tested it with John Smith blah blah blah, and then i went to delete John smith, the function ran through but the entry was still in the text file.
So how can I override just that line in the text file?
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:45 AM

You can't change the contents of the file line by line like I think you are suggesting.

Bring the file contents into memory.
Change the bits you want to change.
Overwrite the old file contents with the new, changed, file contents.
Was This Post Helpful? 0
  • +
  • -

#7 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:49 AM

View Postjanotte, on 4 Jun, 2009 - 02:45 AM, said:

You can't change the contents of the file line by line like I think you are suggesting.

Bring the file contents into memory.
Change the bits you want to change.
Overwrite the old file contents with the new, changed, file contents.


How to i bring the contents into memory and then search through them?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 03:56 AM

Have a read of the examples at the link provided.

And have a look at the tutorials to your right --->>>
Like this one
http://www.dreaminco...wtopic13930.htm
Was This Post Helpful? 0
  • +
  • -

#9 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:08 AM

View Postjanotte, on 4 Jun, 2009 - 02:56 AM, said:

Have a read of the examples at the link provided.

And have a look at the tutorials to your right --->>>
Like this one
http://www.dreaminco...wtopic13930.htm


I've spent about an hour reading, but I still don't see how to overwrite data. :(
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:16 AM

View Postl0ckz0r, on 4 Jun, 2009 - 04:08 AM, said:

I've spent about an hour reading, but I still don't see how to overwrite data.


From here
http://www.cplusplus...tutorial/files/

"ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one."
Was This Post Helpful? 0
  • +
  • -

#11 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:25 AM

View Postjanotte, on 4 Jun, 2009 - 04:16 AM, said:

View Postl0ckz0r, on 4 Jun, 2009 - 04:08 AM, said:

I've spent about an hour reading, but I still don't see how to overwrite data.


From here
http://www.cplusplus...tutorial/files/

"ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one."


Yeah I looked at that, But that makes it seem like it deletes the entire content of a file. Can it be manipulated to delete specific content?
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:27 AM

Look at posting #6 in this thread.
Was This Post Helpful? 0
  • +
  • -

#13 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:33 AM

Okay So am i understanding correctly when you say the solution is to:

Bring the WHOLE txt file into memory
Then edit an entry
Then overwrite the WHOLE file?

I'm supposed to allow the user to search for the line they want to delete (or in this case overwrite) I'm not quite sure how i would go about implimenting it this way.
Was This Post Helpful? 0
  • +
  • -

#14 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:58 AM

Okay So i tried to implement this, but it hasn't worked:

void deleteEntry()
{

	//Get a user entered search term
	//Get each line of the txt file and place it in an array
	//Get the line that matches the search term and place it in a different array
	//Turn that array into \0 charachter
	//Re write the 1st array to a new file that overrides the other one using ios::trunc

	char delSearch[99];
	int i = 0;
	char line[500];


	cout <<"Enter the name you with to delete: ";
	cin.getline(delSearch, 99);

	fstream bookcopy ("bookcopy.txt", ios::in);

	while (!bookcopy.eof())  //while bookcopy is not at the end of the file
	{
		bookcopy.getline(line,sizeof(line)); //Get a line from bookcopy and put it into the line array

		if(strstr(line, delSearch) != NULL) //Compare
		{
			for(i = 0; i < sizeof(line); i++) //Turns the line into \0 charachters
			{
					line[i] = '\0';
			}
			
		}
		else
		{
			for(i = 0; i < sizeof(line); i++)
			{
				fstream bookcopy ("bookcopy.txt", ios::trunc);
				bookcopy << line[i];
			}
		}
	}




	return;
}


Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Deleting data from a txt file

Posted 04 June 2009 - 05:58 AM

Yes, read the file into memory means the file (as opposed to part of the file).

Well, you have a range of options.

One, fairly easy, way would be to read the file line by line into an array or vector (vector is preferred).
Then search the string in each element of the vector for the text you want to find.
When you find a match delete that element in the vector.
Write the new vector element by element (line by line) back to the original file (overwriting the previous contents).

Or use stringstreams or <your design choice here>.

http://www.cplusplus...nce/stl/vector/
http://www.cplusplus...m/stringstream/
http://www.cplusplus...string/getline/
http://www.cplusplus...ng/string/find/
http://www.cplusplus.../string/string/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2