Displaying C debugger output in VS2012 ? - c

So I wrote a POSIX Threads application in C (linux) in which you get as command line args the number of threads , and the filename which the threads will work on.It works.
Now I also need to make it work in windows.
I've changed all the necessary names and headers , but since I'm trying to do it in VS2012 , I don't know how to get the Debugger output.
printf does not work , obviously , so what can I use instead ?
The code is included in a C++ WinConsoleApp project.

You can use OutputDebugString function, but the VS output window should work only for debugger output. For console output, you should create Windows 32 Console Application project.
Other way is to allocate console yourself:
AllocConsole();
freopen("CONIN$", "r",stdin);
freopen("CONOUT$", "w",stdout);
freopen("CONOUT$", "w",stderr);

Related

.exe files dissapear when they want in VS code

Things that i should note are that I am using the latest version of vs code to write in c, my compiler is mingw-w64. Whenever I run a simple code (Hello world for example) everything works then suddendly when I try to do something else a message appears saying (program name).exe was not found. This generally happens when I try to use commands from other libraries, i tries using conio.h textcolor() but it doesn't want to, but when i use getch() it works fine. Moreover the .exe file exists, then i add a single line of code and boom suddendly it doesn't exist. Can anyone explain to me what is going on??

C language - (compile and run but command (cmd.exe) does not appear

I am practising C but I am having problem with this.
I am using wxDev C++ and I ran the file (it says compiles and runs fine) but the problem is that the result does not appear at all (command prompt (cmd.exe) is not appearing).
I am currently using Windows 7 using MacBookPro (as bootcamp).
What can be the potential solutions toward this problem?
Thank you very much
e.g. even this simple code
int main(void){
printf("Hello, world");
getch();
}
The getch() function is waiting for you to press a key before it returns control to the program. The program will not finish executing until you type 1 character.
See http://www.programmingsimplified.com/c/conio.h/getch , which shows a similar program to yours.

eclipse cygwin printf no output on debug

I managed to get cygwin to work with eclipse and made the default hello world program. Running the program is fine and it outputs correctly in console. But when I debug and step through the program, printf statements does not appear in console. Is there a setting somewhere that I need to change?
You should add this once or after every printf
fflush(stdout);
For reasons I don't really know, the program output will occur only at the end of the program. Putting this call will force the output everytime there is a printf

Using Visual Studio 2010, how do I provide input to a Visual C++ Win32 Console Application?

I am using Visual Studio 2010 Pro for simple C programming, I would like to know how I can provide input to the program without having to manually do so. The enviroment I am used to working is your standard commandline Unix enviroment. Once I compile a C file call "inputsInts" it becomes "a.out" and to test input I would type:
The easy way
echo 1 2 3 4| ./a.out //to provide input
The number of ints input was 4 //output
The easier way
more input.txt| ./a.out //to provide input
The number of ints input was 4 //output
The tedious way
./a.out
//now I would manually type
1 2 3 4 s //in this case I have to type a letter to move on
The number of ints input was 4 //output
Hard is how I have to do it in Visual Studio 2010. I would like to be able to just input in an area the input ahead of time or at least have it read a text file. Obviously I can't test large sets of data by manually typing it in. At the moment I am just doing the coding in VS2010 and going to the unix enviroment to do most testing. I would like to stay in the VS2010 enviroment until I am ready to do a final test in Unix.
I have altered the question quite a bit since I first posted, so the original answers may seem off a bit. Again I appretiate everyone's time and help.
This is just the simple code for an example:
#include
int main () {
int x, n = 0;
while (scanf("%d", &x)==1)
n++;
printf("The number of ints input was %d\n", n);
return(0);
}
You need to create a "Console Application" when you start a new Visual Studio project. This will give you a program that runs from a Windows Command Prompt window, otherwise known as the Cmd window after the name of the shell program that runs underneath it. The command window is located under Programs->Accessories in Windows XP, not sure about other versions of Windows. Once you open a command window, things will work similarly to what you're used to on Linux.
cd MyProject
echo 1 2 3 4|.\MyProject.exe
MyProject.exe <input.txt
The cmd.exe shell has a pipe operator that works similar to the Unix pipe operator. It has some quirks in some versions of Windows but in general, you should be able to do many of the same things with it.
You can run your program pretty much the same way at the Windows command line, the only obvious difference being that you need to specify the correct executable name instead of a.out.
To do roughly the same from inside the VS IDE, you'd probably need to store your sample input in a text file, and then specify something like < sample.txt as the arguments to supply to the program in the project's debug settings.
In the Windows Shell (cmd.exe) you can use a pipe similar to linux for commands like
dir|more
Outside the shell, you're talking about a GUI environment (as in Linux's GUI) so passing info from one program to the next will be a bit different.
However, you can achieve similar functionality using pipes (shared memory in Windows). See here for a full explanation with examples from the folks at Microsoft Developer Network:
http://msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx
Or if you're feeling to lazy to poke around, here's an example of transactions on named pipes:
http://msdn.microsoft.com/en-us/library/aa365789%28v=VS.85%29.aspx
...or you can simply dump and read from output files.
(Both of these methods are similar to those used with Linux programs)
More info on your specific needs would be helpful.

How do I stop a command prompt from appearing in a Win32 C application?

I really have no idea why this is happening...
I created a win32 application in emacs, and whenever I make it and run it (not through the command prompt), a command prompt window pops up under the window. If I build a win32 application in Code::Blocks, and just run its default template, then it runs without a command prompt window. I looked all through the code of both and can't figure out what might cause this...
I thought it was because I included some printf() statements in there, but I didn't want them to stay there (they were for debugging), so I wrote a macro:
#define DEBUG
in main.c, and
#ifdef DEBUG
#include<stdio.h>
#define DBG printf
#else
#define DBG
#endif
in a header (included after the #define DEBUG of course).
When I undef'd DEBUG, the window still showed up... I don't know what I am doing to make it happen, what sorts of things cause a command prompt window to show up in a win32 application? I tried using all the ****Ex() windows functions instead of just CreateWindow(), etc, but that didn't change anything. I checked and re-checked the class definition and registration, to no avail, and made sure I didn't forget a printf() statement (which would have caused an error even if I did, since stdio.h isn't included unless DEBUG is defined).
This is what I included:
#include<windows.h>
#include<windowsx.h>
#include"main.h"
#include"windowproc.h"
anyone know what can cause this? I even commented out all of my stdio, DBG, printf junk, and it still showed up, and I swear there's no difference between my code and the Code::Blocks generated code, aside from my use of HANDLE_MSG and a few extra functions to split up the code.
Update
I am using MinGW 3.4.5.
using the -mwindows switch worked, thanks
Try linking with the -mwindows switch.
Your program should also have the main method read like so:
int WINAPI WinMain(
....
)
as opposed to the traditional int main().
Related threads:
Why does my QT4.5 app open a console window under Windows?
GCC / C how to hide console window?
Windows makes a difference between "console" and "Windows" applications. Console applications will always be run with an associated terminal.
See this question for details on the differences.
Don't know what compiler you are using but I know GCC needs the -mwindows option to suppress the command line window.

Resources