hi,
Would anyone show how to delete a specific line from a txt file? Is there a member function from the ios class? thank you.

Recommended Answers

All 8 Replies

>Is there a member function from the ios class?
Sadly, no.

>Would anyone show how to delete a specific line from a txt file?
Copy the file line by line and ignore the line you want to delete. Then delete the old file and rename the copy to the original. The problem is that files generally aren't record oriented, so you don't have the option of moving records around like you do with an array.

You have to copy all the lines you want to keep to a new file.
Then delete the old file, and rename the new file.

You have to copy all the lines you want to keep to a new file.
Then delete the old file, and rename the new file.

is it possible to show how to delete the old file and rename the new file? what class should I use to do this? thank you

>>what class should I use to do this?
Its not a c++ class, just call standard C library functions rename() and remove()

thx for everybody's great help. I have successfully got all the lines i need written into a new file. And I figured out the correct syntax to remove and rename... however,,, last step, it is "permission denied"

Can anyone please tell me what makes it permission denied? I don't have the text document open when running the program.

please post code. does your user login account have admin privaledges? Can you delete the files manually using Windows Explorer?

yes, i can manually delete or rename the text doc. function remove() is my problem. Thank you

/*
	 *C++ review lab_1
	 */
	# include <iostream>
	# include <string>
	# include <fstream>
	# include <iomanip>
	# include <cstdio>

	using namespace std;
	class Name
	{
	public:
		int id;
		string lastName;
		string firstName;
		string phone;
		double amt;
		void output();     // a function to output all the member variables of object Name
		void set(int, string, string, string, double);  // a function to set individual object's member variable
	
		

	};


	////////////// main//////////////////////


	void showlist();  // to show the list of names in a regular order
	void showlistBackWard();  // to show the list of names in a backward order
	void addName();// to add a new name in to the list
	void remove();  //to remove a name
	void sortID();  // to sort the id into accending order
	void accending(int numbers[], int num);
	int smallest_index(const int numbers[], int startIndex, int num);
	void swap(int& v1, int& v2);
	int trashLine(string target); // a help method to return the line of name going to be removed
	int counter(); // to count how many elements are there in the text file

	int main()
	{
		char ans;
		
		do{
			int option;
			cout<<"Enter 1, 2, 3, 4, or 5 for options"<<endl;
			cout<<"1.Show list of names"<<endl;
			cout<<"2.Show list of names in backward order"<<endl;
			cout<<"3.Add a new name on the list"<<endl;
			cout<<"4.Delete a name"<<endl;
			cout<<"5.Show ID in accending order";
			
			cout<<endl;
			cin>>option;
		
			switch(option)
			{
			case 1:
				showlist();
				break;
			case 2:
				showlistBackWard();
				break;
			case 3:
				addName();
				break;
			case 4:
				remove();
				break;
			case 5:
				sortID();
				break;
			default:
				cout<<"You have entered an invaild option.";

			}
			cout<<"Would you like to go back to the main manuel? (type 'y' for yes and 'n' for no)"<<endl;
			cin>>ans;
			
		}while(ans=='y' || ans=='Y');


		
		

		return 0;
	}



	
	void showlist()
	{
		Name name[300];
		
		ifstream reader;
		reader.open("names.txt");

		int i=0;
		while(reader>>name[i].id>>name[i].lastName>>name[i].firstName>>name[i].phone>>name[i].amt)
		{
			i++;
		}

		cout<<"There are total "<<i<<" people in the list."<<endl;
		cout<<endl;
		
		for(int x=0; x<i; x++)
		{
		name[x].output();
		
		}
	}
	

	void showlistBackWard()
	{
		Name name[300];
		
		ifstream reader;
		reader.open("names.txt");

		int i=0;
		while(reader>>name[i].id>>name[i].lastName>>name[i].firstName>>name[i].phone>>name[i].amt)
		{
			i++;
		}

		cout<<"There are total "<<i<<" people in the list."<<endl;
		cout<<endl;

		for(int x=i-1; x>-1; x--)
		{
			name[x].output();
		}
	}

	
	void addName()
	{
		Name newName;
		int inID;
		string inLastName, inFirstName, inPhone;
		double inAmount;
			cout<<"What is the person's ID?";
			cin>>inID;
			cout<<endl;
			cout<<"What is the person's last name?";
			cin>>inLastName;
			cout<<endl;
			cout<<"What is the person's first name?";
			cin>>inFirstName;
			cout<<endl;
			cout<<"What is the person's phone number?";
			cin>>inPhone;
			cout<<endl;
			cout<<"What is the person's total amount?";
			cin>>inAmount;
			cout<<endl;
		
		newName.set(inID, inLastName, inFirstName, inPhone, inAmount);

		ofstream writer;
		ifstream reader;
		writer.open("names.txt", ios::app);
		writer<<newName.id<<"\n"<<newName.lastName<<"\n"<<newName.firstName<<"\n"<<newName.phone<<"\n"<<newName.amt<<endl;
		cout<<endl;
		
		writer.close();

	}


	void remove()
	{	
		string id;
		cout<<"Give me the person's ID that you want to delete."<<endl;
		cin>>id;
		

		
		int trashStarting = trashLine(id);
		int trashEnding = trashStarting+4;
		int beforeTrash = trashStarting-1;
		int afterTrash = trashEnding+1;
		
		int totalLine = counter();
		
		ifstream collector;
		collector.open("names.txt");
		ofstream writer;
		writer.open("updateName.txt");
		
		string goodie;

		//read and write the lines before the target line
		for(int a=0; a<=beforeTrash; a++)
		{
			collector>>goodie;
			writer<<goodie<<"\n";
		}

		//skip lines to next id spot
		for(int b=0; b<5; b++)
		{
			collector>>goodie;
			
		}
		
		
		int restOfLine = totalLine- trashEnding;

		// read and write the rest lines
		for(int c=0; c<restOfLine; c++)
		{
			collector>>goodie;
			writer<<goodie<<"\n";
		}

		int resultRemove =remove("names.txt");  //remove the old file
		if(resultRemove==1)
		{
			puts("removed");
		}

		else
		{
			perror("error");
		}

		int resultRename;        //rename the new file
		char oldname[]= "updateName.txt";
		char newname[]= "names.txt";
		resultRename =rename(oldname, newname);
		
		if(resultRename==0)
		{
			puts("good");
		}

		else
		{
			perror("error");

		}


		
	}
	//help function for remove function returning which line is the start line of the removing line
	int trashLine(string target)
	{
		ifstream reader;
		string thing;

		reader.open("names.txt");

		int count=0;
		while(reader>>thing)
		{
			if(thing==target)
			{
				return count;
			}

			count++;
		}
	}


	int counter()
	{
		ifstream reader;
		string thing;
		reader.open("names.txt");

		int count=0;
		while(reader>>thing)
		{
			count++;
		}

		return count;
	}


	void sortID()
	{
		Name name[300];
		int holder[300];
		ifstream reader;
		reader.open("names.txt");

		int i=0;
		while(reader>>name[i].id>>name[i].lastName>>name[i].firstName>>name[i].phone>>name[i].amt)
		{
			i++;
		}

		for(int x=0; x<i; x++)
		{
			holder[x]=name[x].id;
		}
		
		accending(holder, i);

		for(int j=0; j<i; j++)
		{
			cout<<holder[j]<<endl;
		}
		
	}


	////////////////// help functions for sorting/////////////

//this is a function to sort the array into an accedning ordered array
	void accending(int numbers[], int num)
	{
		int small;
		for(int index=0; index<num-1; index++)      //from the first index, up to the last index, 
		{
			small= smallest_index(numbers, index, num);     //find the smallest value index by using the smallest_index function
			swap(numbers[index], numbers[small]);			// swap the first index with the smallest value index
		}													//and then swap the second index with the smallest index searching from the third index
			
	}


//this is a function to find the smallest value index 
	int smallest_index(const int numbers[], int startIndex, int num)
	{
		int min = numbers[startIndex];
		int indexOfMin= startIndex;

		for(int index=startIndex+1; index<num; index++)    //starting from the first index +1, if it is smaller, assign the value to be the min; and its index to be the smallest index
		{
			if(numbers[index]<min)
			{
				min=numbers[index];
				indexOfMin= index;
			}
		}
			return indexOfMin;  //return the smallest value index after all the looping 
		
	}


	// this is a function to swap two values or array index
	void swap(int& v1, int& v2)
	{
		int temp=v2;
		v2=v1;
		v1=temp;
	}

	////////////////////end of sort id help method //////////////////////
	

////////////////// class memeber functions /////////////////////////////

	//to output all the class's variables
	void Name::output()
	{
	
		cout<<id<<" "<<lastName<<" "<<firstName<<" "<<phone<<" "<<amt<<endl;
	}

	//to set values for class variable
	void Name::set(int inputID, string inputLastName, string inputFirstName, string inputPhone, double inputAmount)
	{
		id= inputID;
		lastName = inputLastName;
		firstName = inputFirstName;
		phone= inputPhone;
		amt = inputAmount;
	}

1. You need to close the files before attempting to rename and remove them.

2. You have a function called remove() which is far too similar to the standard API call for removing files.
Perhaps call you function removeName() ?

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.