Removing data from file

Why does the name I entered wont delete from the file

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
	char delUser[50];
				 ifstream testing;
			     testing.open("Session.txt");
			     testing.clear();
				  ofstream temp;
				  temp.open("temp.txt", ios::out|ios::trunc);
				  GetWindowText(Username,delUser,50);
				  string name(delUser);
				  string line;
				  while(testing.good())
				  {
				  	getline(testing, line);
				  	if(testing)
				  	{
				  		if(line!=name)
				  		{
				  		temp<<line;
				  		temp<<"\n";
				  		}
					  }
					}
				testing.close();
				temp.close();
				remove("Session.txt");
				rename("temp.txt","Session.txt");
Last edited on
How does Session.txt change (or not change) after you run your program?

Factor out your logic so that OS-specific calls like GetWindowText are a separate issue from the your file editing. Are you sure that the line in the file exactly matches the name string?

This works OK for me (intentionally trying to leave it as close as possible to your function, although I simplified it a little):
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
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

std::string getUser()
{
	// You could call GetWindowText(Username,delUser,50); here
	return "Eddy";
}

int main()
{
	const char* filename = "Session.txt";
	const char* tempname = "temp.txt";

	string name = getUser();
		
	ifstream testing(filename);	
	ofstream temp("temp.txt", ios::out|ios::trunc);

	string line;
	while(getline(testing, line))
	{
		if (line!=name)
		{
			temp<<line;
			temp<<"\n";
		}
	}
	
	testing.close();
	temp.close();
	
	remove(filename);
	rename(tempname, filename);
}


Session.txt before running program:
George
Mary
Sue
Eddy
Bobby


Session.txt after running program:
George
Mary
Sue
Bobby


When dealing with files, the first thing I would check is to make sure the input file opened correctly.

Do:
1
2
3
4
5
ifstream testing("Session.txt");
if (!testing)
{
    cout << "ERROR: COULD NOT OPEN FILE\n";
}
Last edited on
Its still not working I also use the checker if the file is not open but no error pops up. The string works because I used it in saving to file.
Last edited on
here is my new code

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
string Getuser()
{
	char delUser[50];
	GetWindowText(Username,delUser,50);
	string name(delUser);
	return name;
}

//-----------------------------MALL PROCEDURE---------------------------------------------------------------//
LRESULT CALLBACK windowProcedure2(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{

	switch(msg)
	{
		case WM_COMMAND:
		
		switch(wp)
		{
			case BUTTON_LOGOUT:
				
				//delete from file
			const char* filename = "Session.txt";
			const char* tempname = "temp.txt";
			
			string cname=Getuser();
				
			ifstream testing(filename);
			if (!testing)
			{
   			 MessageBoxW(hWnd, L"Cannot open the file", L"Error", MB_OK| MB_ICONEXCLAMATION);
			}	
			ofstream temp("temp.txt", ios::out|ios::trunc);
			if (!temp)
			{
   			 MessageBoxW(hWnd, L"Cannot open the file", L"Error", MB_OK| MB_ICONEXCLAMATION);
			}	
			string line;
			while(getline(testing, line))
			{
				if (line!=cname)
				{
					temp<<line;
					temp<<"\n";
				}
			}
	
			testing.close();
			temp.close();
	
			remove(filename);
			rename(tempname, filename);
				
				MessageBoxW(hWnd, L"Successfully Log out", MB_OK,MB_ICONINFORMATION);
				ShowWindow(hLoginWindow,SW_HIDE);
				ShowWindow(hMainWindow,SW_SHOW);
				
			

I found the problem I think it has to do with my text file since inside it is
 
Username Password
Can your usernames or passwords have spaces (whitespace) in them?
Last edited on
Yeah I think it has do with white spaces
Show excerpts of your text file then and we can tell you how to parse your file. How do you differentiate the end of the username from the start of the password?
My text file looks like this and I differentiate it with the use of space. Id like to delete the whole line if for example the name I was looking to delete is George.
1
2
3
George 1234
Allen 4234
Ramon 4444


Last edited on
So then, neither the username nor the password themselves will have spaces, correct?

Anyway, if you are only after the username, you have a few options. The easiest way would probably be to just check the substring of the line to see if does not begin with the username.

So you'd replace
1
2
3
4
if (line != cname)
{
    // ...
}


with
1
2
3
4
5
// + " " needed to differentiate "Bob" and "BobSomething"
if (!begins_with(line, cname + " "))
{
    // ...
}


where begins_with could be implemented as:
1
2
3
4
5
// https://stackoverflow.com/questions/1878001
bool begins_with(const std::string& str, const std::string& prefix)
{
    return (str.rfind(prefix, 0) == 0);
}


Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <string>

bool begins_with(const std::string& str, const std::string& prefix)
{
    return (str.rfind(prefix, 0) == 0);
}

int main()
{
    using std::cout;
    cout << begins_with("Name Password", "Name ") << '\n';
    cout << begins_with("Name Password", "OtherName ") << '\n';
    cout << begins_with("Name Password", "Name2 ") << '\n';
}
Last edited on
In C++20 if that can be used, std::string has the method .starts_with()

https://en.cppreference.com/w/cpp/string/basic_string/starts_with
Last edited on
Topic archived. No new replies allowed.