Deleting text?

Hi, forum i search on google if there's any type of deleting text from cmd, for example: i write a lot of "car" and i want to delete the last 2 words or last 7 letters (inclusive white space's), there's any funtions for this? Thanks in advance, sorry about my english!

1
2
3
4
cout<<"an car is just an"<<endl;
cout<<"car"<<endl;
cout<<"car"<<endl; //How to delete-Those?
cout<<"car"<<endl; //             - 
In other words, your program has already written something to standard output and now you want to do something for the characters that "have already left the building"?

There is a fundamental problem: you don't know what is "out there".

Lets say that the name of your program is foo.exe. You might think that the user runs it in a shell:
foo.exe

The shell hands out the output to terminal window, which draws something to the screen.

That case has some solutions. The problem is that different terminals do not have common API. There are third-party libraries like ncurses that your program can talk to and who can talk to several terminal types.


However, I could run your program:
foo.exe > file.txt

or
while read LINE ; do echo ${LINE} ; done < <(foo.exe | sort)

In neither case does the output appear on the terminal screen as such. If your program nevertheless "wipes the screen", it will not wipe what it wrote out a moment earlier but something else that happened to be on the screen.


Anyway. Seek out the ncurses. It or a variant of it is probably available for your platform.
I just want a funtion to delete the output letters, thanks in advance!
Delete them from what?

Did you not understand keskiverto's post? The point is that, for all you know, those letters might not have been output to the screen. The user might have redirected the output somewhere else.
@MikeyBoy,

as I undestand he wants to delete a number of characters on the screen, probably the one's that were last written.
The point is that, for all you know, those letters might not have been output to the screen. The user might have redirected the output somewhere else.

I guess this is a kind of execise, not a real world app.

@CosminPerRam
What you can try is:
Get current cursor position , move 7 chars back and output 7 spaces.
If you use Windows you can use:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683163%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx


BTW. It's always good to mention what OS and compiler you use.
as I undestand he wants to delete a number of characters on the screen, probably the one's that were last written.

That was my understanding initially. But then after keskiverto explained why that didn't really make sense, he asked again, so I though that it was worth getting some clarification.
Thomas1965, Im on Windows 10 x64 Pro, compiler: codeblocks, i dont know how to use those functions from msd, you can give me an example?
MikeyBoy, i want to display in console a text after to delete a part of it, i dont want to use read fom a file, but thanks for try

I hope i will fix it, i also seached on google but i didn't found anything.
Here is a little example to output some text at col 10 and line 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <Windows.h>

using namespace std;

int main ()
{

  COORD coord = {10, 10};

  SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);

  cout << "Hello from 10, 10" << "\n\n";
  
  return 0;
}


Some of these Windows functions are not so easy to use. Maybe you can wrap the code into a simple GotoXY(short x, short y) function.
Here is another example with some wrapper functions:
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
#include <iostream>
#include <Windows.h>

using namespace std;

short WhereX ()
{
  CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
  GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &csbi);

  return csbi.dwCursorPosition.X;
}

short WhereY ()
{
  CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };
  GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &csbi);

  return csbi.dwCursorPosition.Y;
}

void GotoXY (short x, short y)
{
  COORD coord;

  coord.X = x;
  coord.Y = y;

  SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
}

int main ()
{

  GotoXY (10, 10);

  cout << "Hello from " << WhereX() << "," << WhereY() << "\n\n";
  
  system ("pause");
  return 0;
}
I resolved my problem, thanks also for those useful commands!
Topic archived. No new replies allowed.