Delete the content of file?!

Hi every one!
I need to delete the content specific text in txt file within C++
I have use the code following but it delete all content of it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case 5:
		{
				string line, name;
				cout << "Please Enter the name of record you want to delete: ";
				cin >> name;
				ifstream myfile;
				ofstream outfile;
				myfile.open("StudentInformation.txt");
				outfile.open("StudentInformation.txt");
				
				while (!myfile.eof()) {
					myfile>>line;	
					if(line==search){
						if(line==";")
						cout<< myfile.eof() << endl;
						break;
					}
				}
				
				cout << "The record with the name " << name << " has been deleted if it exsisted" << endl;
				myfile.close();
				outfile.close();
			
		}


for example if i have those content in the file:

Name:Sivar
ID:1
Kurdish:4
Arabic:76
English:87
Mathematic:67
Physics:65
Biology:54
Chemical:87
Average = 62
;

Name:Jack
ID:2
Kurdish:67
Arabic:89
English:67
Mathematic:65
Physics:76
Biology:87
Chemical:89
Average = 77
;

Name:Sandy
ID:3
Kurdish:34
Arabic:65
English:76
Mathematic:89
Physics:65
Biology:54
Chemical:98
Average = 68
;





Then if I type "Jack"'s name , it delete jack's information and the content of file becomes as bellow!

Name:Sivar
ID:1
Kurdish:4
Arabic:76
English:87
Mathematic:67
Physics:65
Biology:54
Chemical:87
Average = 62
;

Name:Sandy
ID:3
Kurdish:34
Arabic:65
English:76
Mathematic:89
Physics:65
Biology:54
Chemical:98
Average = 68
;

Hi,
If you wan to delete a line containing a string "search" you can use getline:

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
#include <string>
#include <fstream>

int main()
{
	using namespace std;
	string input;
	string search = "John";
	fstream file;

	file.open("file_name.txt", ios::in);

	if (file.is_open())
	{
		while (!file.eof())
		{
			getline(file, input);
			if (input.find(search) != string::npos); // check if "John" is found
			{
				// perform delete operation here
				break;
			}
		}
                file.close();
	}
	return 0;
}
Last edited on
It's because you are opening the file twice with 2 different file streams. Just use 1 std::fstream.
Last edited on
@codekiddy
@benbalach
I know how to search for a specific name information and output it, but my problem is that how to delete them, i don't know which code are using to do this,, or i haven't any idea how to delete them?!!!!
Last edited on
Hi sivar,

following example will show you how to delete an entry from the file, however you'll need to redesign it for your needs.

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
#include <string>
#include <fstream>
#include <iostream>

int main()
{
	using namespace std;
	string input;
	string changed_content;
	string search = "John";
	fstream file;
	string file_name = "C:\\path\\to\\file\\file.txt";

	file.open(file_name, ios::in);

	if (file.is_open())
	{
		while (!file.eof())
		{
			getline(file, input);

			if (input.find(search) == string::npos) // check if "John" is not here
			{
				changed_content += input;
				changed_content += '\n';
			}

			input.clear(); // delete input for new loop
		}
		file.close();
		file.open(file_name, ios::out | ios::trunc); // reopen file and clear it's contents
		if (file.is_open())
		{
			file << changed_content; // wite stuff back into the file without john
			file.close();
		}
		else
		{
			cout << "file for rewrite not open\n";
		}
	}
	else
	{
		cout << "file for input not open\n";
	}
	return 0;
}

Last edited on
Topic archived. No new replies allowed.