Deleting data from a txt file

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 06:05 AM

That's sort of what i tried to do in my latest attempt with no luck. Any chance you point out what i'm doing wrong?
Was This Post Helpful? 0
  • +
  • -

#17 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 - 06:17 AM

Where do you read the file into an array/vector line by line?
Hint: You don't.

Why are you using C-style character array strings?
You are writing in C++ so why not use C++ strings?

Try writing out what your code does in English.
When you understand what the code you posted is doing contrast it with what you want it to do (or what I suggested if you want to do that).


EDIT
I'm going offline.
Can someone please take up this thread please.

This post has been edited by janotte: 04 June 2009 - 06:20 AM

Was This Post Helpful? 0
  • +
  • -

#18 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 - 06:20 AM

View Postl0ckz0r, on 4 Jun, 2009 - 05:05 AM, said:

That's sort of what i tried to do in my latest attempt with no luck. Any chance you point out what i'm doing wrong?


Also all those links to things to do with <string> were great. Except I can't use String.

I had this whole program working with string until I found out I couldn't use string, So i've scratched it and started again. I can use fstream, iomanip, cstring, iostream and a few others that probably wont be of any use.
Was This Post Helpful? 0
  • +
  • -

#19 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 - 06:32 AM

Thanks for all your help so far :D

I'll start from step 1

How do I load each line into an array (i dont know about vector so i wont go there)

bookcopy.getline(line,sizeof(line));
Now i thought this loads a line from the text file into the array line

Could I use something like

bookcopy.getline(line[i],sizeof(line));
To load each line into a different element? Or am i doing it wrong?
Was This Post Helpful? 0
  • +
  • -

#20 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:12 PM

View Postl0ckz0r, on 4 Jun, 2009 - 05:20 AM, said:

Also all those links to things to do with <string> were great. Except I can't use String.


When you say you can't use C++ strings do you mean that you are not allowed to use it for this assignment?
Or do you mean something else?

-----

To read each line into an array you would need an array that has as at least as many elements as there are lines in the file.
Since you are using C-style strings (character arrays) you would need an array of arrays (a two dimensional array).
Reading one line into a character array and reading all the lines in a file into an array of character arrays are different ideas.
Don't get confused between the two.
But before you rush into trying to do that read on.
You may not need to do any of this.

-----

More to the point:

Don't start from Step 1 if you already have a working answer available to work from.
If you had this working with C++ strings it should be fairly trivial to simply go back to that code and only change your use of C++ strings to C-style character arrays.
Were you using any features of the C++ <string> library that are not available in the <cstring> library? What were those functions that you need to change?

Can you show us the C++ string code that was working?
I am assuming you didn't use an array of C++ strings to do this when you had it working so don't introduce any new ideas unless you have to.
Better to build upon your own work that you understand than try and change the design to something you got from someone else and don't understand.

----

A side point:
If you are allowed to use vectors then I can't see why not knowing about them before you start would be an argument not to use them.
You seem to have only a partial knowledge of how to use file I/O and C-strings and you are using them.
The point of the assignments your teacher gives you is to learn how to use things you don't know how to use before doing the assignment.
But if your teacher has banned C++ strings then there is every chance they would ban vectors if they thought you would become aware of them.
It's hard to know without the requirements for the assignment.

-----

Because we are guessing at what limits your are operating under we may give you false leads so you need to be careful to only use what is useful to you and ignore what is not useful. We don't know what you know (unless you share it) so beware of well-intentioned but useless suggestions.
Was This Post Helpful? 0
  • +
  • -

#21 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 - 08:24 PM

Okay im trying now to write into an array of structs, copy that into a second array, ignoring the entry to be deleted
Then writing the second array into a file that overwrites the first:

void deleteEntry()
{

	//Get a user entered search term
	//Get each line of the txt file and place it into an array
	//Search the array for matches in the search term
	//Ignore that element of the array (or delete it)
	//Re write the array to a txt file that overwrites the old file

	char delSearch[99];
	int i = 0;
	person array[500];
	person array2[500];


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

	ifstream bookcopy ("bookcopy.txt", ios::in);
	if(!bookcopy.good())
	{
		cout << "Could not open book file" << endl;
	}

	while (!bookcopy.eof())  //while bookcopy is not at the end of the file
	{
		for (i = 0; i < sizeof(array); i++) // Copies everything into the array
	{
		bookcopy.getline(array[i].name, sizeof(array), '#');
		bookcopy.getline(array[i].address, sizeof(array), '#');
		bookcopy.getline(array[i].phoneNo, sizeof(array), '#');
		bookcopy.getline(array[i].birthDate, sizeof(array), '#');
		
		
		if(strcmp(delSearch, array[i].name)) //Moves everything except the entry to be deleted into array2
		{
				strcpy(array2[i].name, array[i].name);
			strcpy(array2[i].address, array[i].address);
			strcpy(array2[i].phoneNo, array[i].phoneNo);
			strcpy(array2[i].birthDate, array[i].birthDate);
		}
			
	}
	
	} 
	
	bookcopy.close();
	
	ofstream bookcopy2 ("bookcopy.txt", ios::trunc);
	
	for (i = 0; i < sizeof(array2); i++) //Outputs all the remaining entries
	
	{
		bookcopy2 << array2[i].name << '#';
		bookcopy2 << array2[i].address << '#';
		bookcopy2 << array2[i].phoneNo << '#';
		bookcopy2 << array2[i].birthDate << '#';
	}
	
	bookcopy2.close();


	return;
}




It doesnt work... instead it crashes the program. What am i doing wrong?

This post has been edited by l0ckz0r: 04 June 2009 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2