Computing.Net > Forums > Programming > console in VC++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

console in VC++

Reply to Message Icon

Name: Alex
Date: October 1, 2003 at 02:13:08 Pacific
OS: win98se
CPU/Ram: tb@900/128sdram
Comment:

I need to "delete" a ENTER character that I,ve writeen in the console with C++.
in BorlandC I have gotoxy, wherex, wherey, movetetxt and so on. But I work in Visual C by Microsoft. how can I move un a line (by deleting a '\n',or however?



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: October 1, 2003 at 10:49:23 Pacific
Reply:

I don't believe VC supports console modes in terms of character control anymore, so you'd most likely need the use the WINAPI for things like cursor position, but I'm not familiar with the API.

You might give ungetc (stdio.h) a try, but it won't remove an EOF character. There is also putback (iostream) which works on the last character in the current stream.

Post your code, so others might get an idea of what you're trying to do.

HTH


0

Response Number 2
Name: egkenny
Date: October 1, 2003 at 20:52:44 Pacific
Reply:

MSDN Library Visual Studio 6.0
Active Subset: Visual C++, Platform SDK and Win CE Docs
Search for: Console Functions
Look at these topics:
Topic: Console Functions
Topic: Console: Demonstration of the Console Functions

=================================================

Here is an example using Console Functions

test_console.cpp
----------------
#include <windows.h>
#include "console.h"

int main()
{
HANDLE hConsole;
BOOL bSuccess;
char *buffer="Hello World", c;

hConsole = InitConsole();
cls(hConsole);
bSuccess = SetConsoleCursorPosition(hConsole, 12, 40);
bSuccess = PutConsoleString(hConsole, buffer);
bSuccess = SetConsoleCursorPosition(hConsole, 10, 30);
bSuccess = PutConsoleString(hConsole, "Up 2 lines");
c = GetConsoleChar();
return 0;
}

console.h
---------
#define BACKGROUND_WHITE (WORD) 0x00f0
#define FOREGROUND_BLACK (WORD) 0x0000
HANDLE InitConsole();
void cls(HANDLE hConsole);
BOOL SetConsoleCursorPosition(HANDLE hConsole, int row, int col);
int PutConsoleString(HANDLE hConsole, char *s);
CHAR GetConsoleChar(void);

console.cpp
-----------
#include <windows.h>
#include "console.h"

HANDLE InitConsole()
{
HANDLE hConsole;
BOOL bSuccess;
hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
bSuccess = SetConsoleActiveScreenBuffer(hConsole);
bSuccess = SetConsoleTextAttribute(hConsole, BACKGROUND_WHITE |
FOREGROUND_BLACK);
return hConsole;
}
void cls(HANDLE hConsole)
{
COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in the current buffer */
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
return;
}

BOOL SetConsoleCursorPosition(HANDLE hConsole, int row, int col)
{
BOOL bSuccess;
COORD p;
p.X=col;
p.Y=row;
bSuccess = SetConsoleCursorPosition(hConsole, p);
return bSuccess;
}

BOOL PutConsoleString(HANDLE hConsole, PCHAR s)
{
BOOL bSuccess;
DWORD cCharsWritten;
BOOL retflag = TRUE;
bSuccess = WriteConsole(hConsole, s, strlen(s), &cCharsWritten, NULL);
if (bSuccess)
retflag = bSuccess;
else
retflag = FALSE;
return(retflag);
}

BOOL PutConsoleLine(HANDLE hConsole, PCHAR s)
{
BOOL bSuccess;
const PCHAR crlf = "\n";
BOOL retflag = TRUE;
bSuccess = PutConsoleString(hConsole, s);
if (bSuccess)
bSuccess = PutConsoleString(hConsole, crlf);
if (bSuccess)
retflag = bSuccess;
else
retflag = FALSE;
return(retflag);
}

CHAR GetConsoleChar(void)
{
HANDLE hStdIn; /* standard input */
DWORD dwInputMode; /* to save the input mode */
BOOL bSuccess;
CHAR chBuf; /* buffer to read into */
DWORD dwRead;

hStdIn = GetStdHandle(STD_INPUT_HANDLE);
bSuccess = GetConsoleMode(hStdIn, &dwInputMode);
bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_LINE_INPUT &
~ENABLE_ECHO_INPUT);
bSuccess = ReadFile(hStdIn, &chBuf, sizeof(chBuf), &dwRead, NULL);
bSuccess = SetConsoleMode(hStdIn, dwInputMode);
return(chBuf);
}


0

Response Number 3
Name: Ronin1
Date: October 2, 2003 at 10:48:59 Pacific
Reply:

egkenny,

Is that portable to other compilers like Dec-C or Code Warrior 5 or is it VC specific? I'd sure like to try it :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Re mysql/php Hiding Tasks in Task Mana...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: console in VC++

Image processing question in VC++ www.computing.net/answers/programming/image-processing-question-in-vc/3437.html

MIDI events in VC++ www.computing.net/answers/programming/midi-events-in-vc/14777.html

Drawing Rectangle in VC++ www.computing.net/answers/programming/drawing-rectangle-in-vc/5863.html