Delete a specific line from a file

I'm new to vb but i'm just making a simple program that stores info, allows one to display the info and also delete it.

This is what i have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using std::string;
using namespace std;

int main()
{	
		int userMenu;
		cout<< "What would you like to do: " << endl;
		//User Menu layout
	do{
		cout<< "1-Store Event(s)" << endl << "2-Lookup Events" << endl << "3-Delete Events" << endl << "4-Exit" << endl;
		cin>>userMenu;
		{
			//Code for User Menu
			switch(userMenu)
			{
				case 1:		//Storing Events in a new file "events.txt
					{
						ofstream events;
						events.open("events.txt", ofstream::app);
						if(events.is_open())
						{
							events << "\n";
							string storedEvent;
							cout<< "What event would you like to log?"<< endl;
							cin>> storedEvent;
							events << storedEvent;
						}
						events.close();
						break;
					}
				case 2:		//Looking up stored events
					{
						system("cls");
						cout << "---Events---"
						string line;
						ifstream events ("events.txt");
						if(events.is_open())
						{
							while (! events.eof())
							{
								getline(events,line);
								cout << line << endl;
							}
							
						}
						events.close();
						break;
					}
				case 3:
					{

			}	
		}
	}
	while (userMenu != 4);
	return 0;
}


From what I can think of there are only two things that I need help doing
When I enter a line (by pushing one) it only tranfers what i'm typing into the file untill there's a space. So if i hit 1, type "hello my name is charlie", the txt file "events.txt" would show
hello.

The second problem is I want to be able to delete what I enter and since i'm storing everything i enter on a new line I was wondering how i'd be able to delete a specific line from the txt file i'm storing my events in. I was thinking maybe transferring the contents of the file to an array and then it would be easy to delete from the array(I'm not sure how i'd do this though). Any help would be appreciated as i'm working on this at my own leisure.
For problem #1 you have to use getline (just as you did for reading the file) instead of cin >> storedEvent.

For problem #2 you've got the idea; you just have to code it. Read the file into an array. You are already reading the file on lines 43-47. Put the 1st line you read into index [0], the second into index [1] and so forth.
This is my new code after attempting to still accomplish the same task:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<vector>

using std::string;
using namespace std;

int main()
{	
	int userMenu;
	cout<< "What would you like to do: " << endl;
	//User Menu layout
	do{
		cout<< "1-Store Event(s)" << endl << "2-Lookup Events" << endl << "3-Delete Events" << endl << "4-Exit" << endl;
		cin>>userMenu;
		cin.get();
		{
			//Code for User Menu
			switch(userMenu)
			{
				case 1:		//Storing Events in a new file "events.txt
					{
						ofstream events;
						events.open("events.txt", ofstream::app);
						if(events.is_open())
						{
							system("cls");
							string storedEvent;
							cout<< "What event would you like to log?"<< endl;
							getline (cin, storedEvent);
							events << storedEvent << endl;
						}
						else cout<< "unable to open file";
						events.close();
						break;
					}
				case 2:		//Looking up stored events
					{
						int i=1;
						system("cls");
						cout << "---Events---"<< endl;
						string line;
						ifstream events ("events.txt");
						if(events.is_open())
						{
							while (! events.eof())
							{
								getline(events,line);
								cout << i++ << "   " << line << endl;
							}
						}
						events.close();
						break;
					}
				 case 3:
					{
						system("cls");
						vector<string> Delete;
						string line;
						fstream events;
						events.open("events.txt", istream::in);
						if(events.is_open())
						{
							while (! events.eof())
							{
								getline(events,line);
								Delete.push_back(line);
							}
							int i;
							for( i=1; i<Delete.size(); ++i)
								cout <<i << "  " << Delete[i] << endl;
						}
						events.close();
					}
			}	
		}
	}
	while (userMenu != 4);
	return 0;
}


I'm mainly focusing on Case 3:

I've stored the file contents in a vector because an array doesn't automatically adjust to the file size like a vector does.
I've displayed the contents of the vector.
Now all i need to do is figure out how to delete a specific line from the vector


So if the vector prints out this
1 Clean car
2 Clean house
3 wash dog

I want to be able to push 3 and have it delete the 3rd line without erasing the other contents of the file. I know i have to save it and reprint it but i'm still i little lost can someone give me a push in the right direction.
I've came up with this so far but I cannot figure what to do anymore. If anyone knows how to do this please help :D.


Here's what i have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<vector>

using std::string;
using namespace std;

int main()
{	
	int userMenu;
	cout<< "What would you like to do: " << endl;
	//User Menu layout
	do{
		cout<< "1-Store Event(s)" << endl << "2-Lookup Events" << endl << "3-Delete Events" << endl << "4-Exit" << endl;
		cin>>userMenu;
		cin.get();
		{
			//Code for User Menu
			switch(userMenu)
			{
				case 1:		//Storing Events in a new file "events.txt
					{
						ofstream events;
						events.open("events.txt", ofstream::app);
						if(events.is_open())
						{
							system("cls");
							string storedEvent;
							cout<< "What event would you like to log?"<< endl;
							getline (cin, storedEvent);
							events << storedEvent << endl;
						}
						else cout<< "unable to open file";
						events.close();
						break;
					}
				case 2:		//Looking up stored events
					{
						int i=1;
						system("cls");
						cout << "---Events---"<< endl;
						string line;
						ifstream events ("events.txt");
						if(events.is_open())
						{
							while (! events.eof())
							{
								getline(events,line);
								cout << i++ << "   " << line << endl;
							}
						}
						events.close();
						break;
					}
				 case 3:
					{
						system("cls");
						int i=0; int delEvent;
						string Delete[100];	string line;
						fstream events;
						events.open("events.txt");
						if(events.is_open())
						{
							while (! events.eof())
							{
								getline(events,line);
								cout <<i++ << "  " << line << endl;
								Delete[i]=line;
							}
						cout<< "Which would you like to delete?";
						cin >> delEvent;
						events.close();								
						}
						//Print out new file	
						{
							system("cls");
							fstream events;
							events.open("events.txt");
							if(events.is_open())
								{
									for ( int b = 0; b < 10; b++)
										{
											getline(cin, Delete[b]);
											events << Delete[b] << endl;
										}
								}
										cout << events;
										events.close();
								
						}
					}
			}	
		}
	}
	while (userMenu != 4);
	return 0;
}
bump
In your Delete string your syntax is wrong. Use a vector to store strings. Then get the event to be deleted and iterate through the vector of strings and find the event. Then use the erase function that comes with the vector class.

Here is some sample code I wrote for another post similar to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
vector<string> file;
string temp;

ifstream infile("Orders.txt");

while( !infile.eof() )
{
	getline(infile, temp);
	file.push_back(temp);
}
// done reading file
infile.close();

string item;

cout << "Enter an item to delete from the order: ";
getline(cin, item);

for(int i = 0; i < (int)file.size(); ++i)
{
	if(file[i].substr(0, item.length()) == item)
	{
					
		file.erase(file.begin() + i);
		cout << "Order erased!"<< endl;
		i = 0; // Reset search
	}
}

//write new order list back out
ofstream out("Orders.txt", ios::out | ios::trunc);

for(vector<string>::const_iterator i = file.begin(); i != file.end(); ++i)
{
	out << *i << endl;
}
out.close();
Last edited on
Topic archived. No new replies allowed.