1. Writing to .txt 2. 'Breaking' Code

I have two questions:

1. Writing to a .txt

I have a program that copies what the input is and puts it in a .txt. It works fine in everything but backspace. The problem is, it writes character-by-character, so it reads \b as a special character, instead of erasing one character back. \t, \n work fine. but backspace doesn't. I know it's because it only reads per character, not by entire string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                       else if (str == "Enter") {
				str = "\n";
				printable = true;
			} else if (str == "Space") {
				str = " ";
				printable = true;
			} else if (str == "Tab") {
				str = "\t";
				printable = true;
			} else if (str == "Backspace") {
				str = "\b";
				printable = true;
			} else {
				str = ("[" + str + "]");
			}

1
2
3
4
  std::ofstream file;
  file.open("C:\\Sites\\Web Key.txt", std::ios_base::app);
  file << str;
  file.close();

Another thing is that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
			if (str == "Caps Lock") {
				capslock = !capslock;
				str = "";
			}
			else if (str == "Shift") {
				shift = true;
				str = "";
			}
....
		if (printable) {
			if (shift == capslock) { /* Lowercase */
				for (size_t i = 0; i < str.length(); ++i)
					str[i] = tolower(str[i]);
			}
			else { /* Uppercase */
				for (size_t i = 0; i < str.length(); ++i) {
					if (str[i] >= 'A' && str[i] <= 'Z') {
						str[i] = toupper(str[i]);
					}
				}

			shift = false;
		}


Doesn't work. Does anyone know how to fix this? It only capitalizes if the Caps Lock is on, or directly after LEFT Shift, or both; Right shift, or holding left shift accomplish nothing.

I have this set up so that people can install and run it on a website. However, I want to make sure they use it. Is there a way to 'break' the html if they don't have this running? Does C++ have a way to redirect if something isn't running, or should I wander into JavaScript territory? It's client-side html code; not an actual web-hosted site.
Last edited on
This does not work
{cout << "Test backspace" << "\b"<< "\b" << "\n";

This does
{cout << "Test backspace" << "\b"<< "\b" << "\b";

Can't say why...

My work around:

1
2
3
4
     {cout << "Test backspace" << "\b" ;
     cout <<  " ";
     cout <<  "\n ";
          }

So does that mean that for
1
2
3
else if (str == "Backspace") {
str = "\b";
printable = true;

I need to use
str = "\b \n ";
it will correctly backspace in .txt?

EDIT: Working with that, I didn't accomplish anything. The output was literally nothing. It seems like you're doing cout << in command line, instead of into a .txt. Also, my problem is that it reads character-by-character, instead of altering the string before the input.


Can anyone find out how to make it so it only prints into the .txt after so many inputs? I.E. it only types in every 100 characters, instead of every character. I think that would fix it, because it can \b things before they enter the .txt, eliminating the problem.
Last edited on
Topic archived. No new replies allowed.