\n won't work with system("cls")

Hey guys,
I'm coding a .dll, and from that .dll, when injected, it opens a PromptCommand window, and there i'll print out some instructions.

First of all, I should say that I coded this in VisualStudio2010, and it ran perfectly. Now I've upgraded to VisualStudio2013, and this problem appears.
I've considered the fact that it might be a VS2013 bug, but I'd like to see all possibilities before I make any other move.

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
#include <iostream>
#include <stdio.h>
#include <Windows.h>

int Foo(){
	// ---------------- CONSOLE ------------------//
	AllocConsole();                               //
	SetConsoleTitle(L"Exercises Foo");            //
	DETACHED_PROCESS;		              //
	freopen("CONOUT$", "w", stdout);              //
	// -------------------------------------------//

	while (!GetAsyncKeyState(VK_F1))
	{
		//system("cls");
		//std::cout.clear();
		std::cout << "F" << std::endl;
		std::cout << "O" << std::endl;
                std::cout << "O" << std::endl;
	}

	FreeConsole();

	return 0;
}

DWORD WINAPI Main_thread(LPVOID lpParam)
{
	Foo();
	return S_OK;
}

BOOL APIENTRY DllMain(HMODULE hModule,
	DWORD  _reason,
	LPVOID lpReserved
	)
{
	if (_reason == DLL_PROCESS_ATTACH)
	{
		CreateThread(0, 0x1000, &Main_thread, 0, 0, NULL);
	}
	return TRUE;
}


Now, when I run this code, as it is, it runs perfectly..

However, when I uncomment system("cls"); or std::cout.clear();, my problems start to begin.

[1] system("cls");
When I uncomment this line and run it, it clears the screen everytime it re-enters the loop.
BUT, it also DOES NOT PRINT OUT A NEW LINE, instead, it prints out 2 unrecognizable characters.
I've tryed setting freopen function to "w", "wb", "wt", and none of them fixed my problems. In fact, one of them (idk which) prints out only 1 unrecognizable character, but it doesn't matter because it didn't solve the problem.

[2] std::cout.clear();
When I uncomment this line and run it, it DOES PRINT A NEW LINE, however, it does not clear the screen.

How do I get it to clear the console & to print out a new line.
[2] std::cout.clear();
When I uncomment this line and run it, it DOES PRINT A NEW LINE, however, it does not clear the screen.
std::cout.clear(); is used for clearing error flags not clearing the console. http://www.cplusplus.com/reference/ios/ios/clear/
http://en.cppreference.com/w/cpp/io/basic_ios/clear

On windows it is "\r\n" not "\n" for a newline. Though std::endl; should work. Also, that is a weird filename "CONOUT$"
http://www.cplusplus.com/reference/cstdio/freopen/?kw=freopen
http://en.cppreference.com/w/cpp/io/c/freopen
Last edited on
@wallpatrol
There is more to associating a stream in Windows than you think. I suspect this has something to do with your problems with system() -- which you should not be using to clear the screen anyway.

Here's some help: http://www.cplusplus.com/articles/4z18T05o/#Windows

By the way. You are aware, are you not, that you are messing with the calling process's console, right?

@giblit
On Windows, any stream opened in text mode will take "\n" for a newline and translate it to the proper CR-LF sequence.

Also, "CONOUT$" is the special Windows filename for the standard output stream.

Hope this helps.

Topic archived. No new replies allowed.