This question already has answers here:
How can I call notepad.exe from a C program?
(3 answers)
Closed 5 years ago.
I just finished making software where given an input of a .jpg file, the program creates an output in .txt format.
Currently, the program creates the output and terminates, so the user has to manually find and open the output file. Is there a way that upon creation of the output file, that the program then opens it automatically?
By this I mean the program would open the output.txt file using notepad, just as if the user double clicked it himself. How would I go about doing this?
One can use the system () command. This invokes the system command processor.
You can give it the command that should be run in the cmd. This is not a efficient solution as system is not good for the efficiency and security of the program, if you are intending to use the code for some commercial build, using system is not recommended, for hobby programs you can surely use it.
Lets say that the file is picture.txt at location C:/file
Your code will be:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
system ("notepad.exe c:/file/picture.txt");
return 0;
}
Related
for an assignment I wrote a program and we hit start without debugging to use the program and the console would ask questions and such. As part of this assignment, we have to write everything that appears in the console window (in visual studio, when we hit start without debugging or ctrl+f5) to a .txt file. Is there a way to have every cout statement write to this file or do I have to type it out on every line that i want to be written to the .txt?
using the ofstream with #include fstream for this by the way
This question already has answers here:
How to stop C++ console application from exiting immediately?
(35 answers)
Closed 5 years ago.
I just started using Visual Studio to write C++ code on Windows 10 and I am using the console application template. My problem is that the console disappears immediately after showing the output of my program even when I simply run a "hello world" example. I tried all kinds of tricks to implement delay but no success.
I usually put a std::cin line just before main returns. This will cause it to wait for input before continuing.
You can do the following( assuming that you are not waiting for user input, in that case you can just follow Rich's answer ) :
Run it in debugging with a breakpoint on the last line before the main returns.
Use a system("pause") at the end before the main returns.( suggesting this just because it's just a hello world program in Visual Studio )
A great way to pause the console in Visual Studios for your purposes is to use system("pause"); Though its not portable to other OS and some anti-virus systems don't like it. For your purposes it should work well and is easy to see what the line does.
Hope this helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've recently read about how BAD system("PAUSE") is. I tried cin.get() but I don't know how to use it to pause a program. Other posts say to put a breakpoint after the statement. But I have no knowledge on how to do that. Any suggestions on how to pause my program? An example would be really appreciated.
Assuming that you are indeed working on Windows, the worst thing about system("PAUSE") is that it betrays a fundamental misunderstanding of your operating system's architecture. You do not need a code replacement for system("PAUSE"), because the code is the wrong place to solve the perceived problem.
Beginners like to put system("PAUSE") or even a portable alternative like std::cin.get() at the end of a program because otherwise "the window disappears" as soon as the program ends. Such logic, however, is deeply flawed. The window which you probably see while the program runs and which has made you ask this question is not part of the program itself but part of the environment in which the program runs.
A typical console program, however, must not assume details about the environment in which it is executed. You must instead learn to think in more abstract terms when it comes to input and output via std::cout and std::cin. Who says that your program is even visible for a human user? You may read from or write into a file; you may use pipes; you may send text to a network socket. You don't know.
#include <iostream>
int main()
{
std::cout << "Hello world\n"; // writes to screen, file, network socket...
}
Opening a graphical window and displaying text output on the screen is not in the scope of your program, yet using system("PAUSE") assumes exactly that one single use case and breaks all others.
If you use an IDE like Visual Studio and are annoyed by the fact that pressing F5 eventually results in the window disappearing before you have had the chance to see all output, here are three more sensible alternatives than manipulating the program itself:
Demystification. Observe that what Visual Studio really does is invoking the Visual C++ compiler behind the scenes in order to create an *.exe file. Open your own console window with cmd or with Tools > Visual Studio Command Prompt, locate the directory of that *.exe file and run it there (you should eventually also learn to start the compiler without Visual Studio's help, because that will give you a deeper understanding of the C++ build process).
Press CTRL+F5.
Place a breakpoint at the end of your code. Read the documentation if you don't know how.
I got one step closer, cin.ignore, but the user can only press enter if he/she wants to continue.
Example with cin.ignore():
#include <iostream>
int main()
{
cout << "Press enter to continue!\n";
cin.ignore();
//do something
return 0;
}
When you press enter it advances and does whatever you want it to do.
This question already has answers here:
Create an Application without a Window
(7 answers)
Closed 8 years ago.
I'm trying to write an exe that runs with out any window how cat I do that?
How can I run/write an exe that will not open any window?
(A code example can be very useful).
Thank you very much!
You'll find a lot on google, for example
https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background
So to answer this shortly, just use the windows command "start" with the parameter B (-> background i suppose).
So write your program and create a small .bat file which includes just
start /B <your program>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have created this program:
//Includes....
int main()
{
ifstream readfile("File.txt");
string str;
while(1)
{
getline(readfile,str);
system("cls");
Sleep(10000);
}
}
It's just a program that reads every 10 seconds a line from the file "File.txt"
I want to make it work on background,how can I do that?
If this was UNIX you would run the program from the shell with an ampersand "&" after the program name.
This sample hide console windows for you :
#include "windows.h"
#include fstream
#include string
#include stdio.h
using namespace std;
int main()
{
WCHAR path[260];
GetModuleFileName(NULL,path,260);
HWND console = FindWindow(L"ConsoleWindowClass",path);
if(IsWindow(console))
ShowWindow(console,SW_HIDE); // hides the window
//---------------------------------------------------
ifstream readfile("File.txt");
string str;
while(1)
{
getline(readfile,str);
system("cls");
Sleep(10000);
}
//----------------------------------------------------
if(IsWindow(console))
ShowWindow(console,SW_SHOW); // shows the window
}
Platform dependent. But I think you are using windows because of the "cls" command.
Why cant you just start a new command prompt and execute the correct .exe file. Then just minimize the program and do what you are supposed to do. If want to automate the startup phase (like cron in linux/unix) use the built in scheduler for windows.
If you want a (non-service) program that doesn't use a console window or any other windows at all, change main() to winmain(). Getting at command line arguments is a little more involved though.
I'm sorry, but I'm not sure I know what you mean... as far as I know (please correct me if I'm wrong) there is no way to run a simple C(++) program without a console window. However if you want it to "sleep" in a way that uses near no resources, then that can be achieved by checking current time and compering it with the time when you completed your last read.
Sleep function usually uses more resources than this, however it works just as well, so if resource cap isn't very very important then it will do just as well.
Hope this helped.
If you're launching your program from a Dos console, you can use the command start \B myprogram.exe, which is more or less equivalent to the unix ampersand. However, you'll need to keep the console open to keep the program running.
Another solution is to convert your application into a windows service, but then you'll have to rewrite (or wrap) your application.
If you want this because you're logging some kind of output and want to view the changes you could just use an editor such as TextPad which will automatically reload the file when it changes.
However you've not specified why you want this so this may not be appropriate in your case.
The proper way is to use a Windows service, there is enough information on Google on how to do this.