I am trying to create a program that auto formats my f:\ (USB Drive), and I have this code so far:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
int main ()
{
system ("format f:");
system("pause");
return 0;
}
Now, when I run that, it comes up, with me needing to press Y, then Enter. How can I simulate that?
As Tyler said in a comment, a batch file would be much easier, but this might work:
system( "echo y | format f:" )
Related
#include <curses.h>
#include <unistd.h>
#include <iostream>
int main() {
initscr();
mvaddstr(10, 10, "Hello, world");
refresh();
sleep(4);
endwin();
std::cout << "DONE\n";
}
I'm working on a project and I need to take down the curses windows for a while just to write a path to directory in cmd and then let the curses windows come back. I found this code on this site and tried to use sleep command in the code but it didn't work.
So if anyone knew how to solve this please write it down here. Thnx :)
napms is the (portable) curses function to use instead of sleep
I'm writing a small CLI application and I want to allow the user to redirect to a file while standard cout statements go to the output.txt file I want progress to always to go the screen.
./myApp > output.txt
10% complete
...
90% complete
Completed
Is this possible? How can I do it?
Thanks in advance!!
This will work even if both stdin and stdout have been redirected:
spectras#etherbee:~$ ./term
hello terminal!
spectras#etherbee:~$ ./term >/dev/null 2>&1
hello terminal!
The idea is to open the controlling terminal of the process directly, bypassing any redirection, like this:
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open("/dev/tty", O_WRONLY);
if (fd < 0 && errno != ENODEV) {
/* something went wrong */
return 1;
}
int hasTTY = (fd >= 0);
if (hasTTY) {
write(fd, "hello terminal!\n", 16);
}
return 0;
}
From man 4 tty:
The file /dev/tty is a character file with major number 5 and
minor number 0, usually of mode 0666 and owner.group root.tty. It is
a synonym for the controlling terminal of a process, if any.
If you're using C++, you might want to wrap the file descriptor into a custom streambuf, so you can use regular stream API on it. Alternately, some implementations of the C++ library offer extensions for that purpose. See here.
Or, if you don't care about getting the error code reliably, you could just std::ofstream terminal("/dev/tty").
Also as a design consideration if you do this, offering a quiet option to let the user turn off the writing to the terminal is a good idea.
Your process cannot know if the shell redirects the standard console output (std::cout) or not.
So you'll need another handle that lets you output to the terminal independently of that redirection.
As #Mark mentioned in their comment you could (ab-)use1 std::cerr to do that, along with some ASCII trickery to overwrite the current output line at the terminal (look at backspace characters: '\b').
1)Not to mention the mess printed at the terminal if the output isn't actually redirected.
You can write your progress indicators to the stderr stream. They will appear on the console if the user redirects stdout to a file.
For example:
fprintf(stderr, "10%% complete\n");
I figured out how to do it, even if the user redirects stderr. The following code gets the name of the current terminal and checks to see if our output is being redirected. It also has a my_write() function that allows you to write to both the terminal and the redirect file, if they've redirected stdout. You can use the my_write() function with the writetoterm variable where-ever you want to write something that you want to always be written to the terminal. The extern "C" has to be there, otherwise (on Debian 9 with GCC 6.3, anyway) the ttyname() function will just return NULL all the time.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <sstream>
using std::string;
using std::fstream;
using std::cout;
using std::endl;
using std::cerr;
using std::stringstream;
void my_write(bool writetoterm, int termfd, string data)
{
if(writetoterm)
{
int result = write(termfd, data.c_str(), data.length());
if(result < data.length()){
cerr << "Error writing data to tty" << endl;
}
}
cout << data;
}
extern "C" {
char* GetTTY(int fd){
//printf("%s", ttyname(fd));
return ttyname(fd);
}
}
int main(int argc, char** argv){
getenv("TTY");
bool writetoterm = false;
struct stat sb = {};
if(!GetTTY(STDOUT_FILENO)){
//not a TTY
writetoterm = true;
}
int ttyfd = open(GetTTY(2), O_WRONLY);
if(ttyfd < 0){
//error in opening
cout << strerror(errno) << endl;
}
string data = "Hello, world!\n";
my_write(true, ttyfd, data);
int num_for_cout = 42;
stringstream ss;
ss << "If you need to use cout to send something that's not a string" << endl;
ss << "Do this: " << num_for_cout << endl;
my_write(writetoterm, ttyfd, ss.str().c_str());
return 0;
}
I found the official std:: method of handling this. There is another type... std::clog. This is specifically for information and always appears on the command line even though the user redirects the output of the program myProgram > out.txt.
Thanks this was great to see all the methods that this can be done.
I've been trying to use OpenCV with C++ but even though my code compiles (Visual Studio 2010 ), it doesn't ever do anything:
#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cvwimage.h"
#include "cxcore.h"
#include "cxmisc.h"
#include "ml.h"
using namespace cv;
using namespace std;
int main()
{
namedWindow("yolo", WINDOW_AUTOSIZE );
waitKey(1);
cout << "Why won't this show up?" << endl;
}
It compiles OK, without errors but the program doesn't do anything - when I open it in console, it doesn't return the 'Why won't this show up?" text - there is nothing returned.
Regardless of which tutorial piece of code I am trying to use, it never works and never does anything.
What is going on?
Best regards
EDIT: When I set the wait time to 0 (forever) it still doesn't work.
The window does get created, however, because you have the waitKey function set to 1 millisecond it only exists for a very short period of time. Try using:
waitKey(0);
waitKey(1); is the culprit. the minimum time is 1 millis (that#s what you got here)
either make it :
waitKey(0); // forever
or increase the time to something sensible
waitKey(5000); // 5 secs
It needs to be,
#include <cv.h>
#include <highgui.h>
cv::namedWindow("test_1", CV_WINDOW_AUTOSIZE );
cvWaitKey();
std::cout << "This will work because I just tested it." << endl;
I use CMake to link the libraries. The CMake code is,
FIND_PACKAGE( OpenCV REQUIRED )
TARGET_LINK_LIBRARIES( myProject ${OpenCV_LIBS} )
Trying to launch a website in Visual C++ 2010 Express, every researched solution has not worked. So I gather I need more in-depth assistance. Here's my code:
#include <stdafx.h>
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <string>
#include <string.h>
#include <conio.h>
#include <windows.h>
using namespace System;
using namespace std;
int main()
{
char name[240];
cout<<"\nHello, Please enter your name: ";
cin.getline(name,240);
cout<<"\nHello "<<name <<", your ID has been confirmed!";
cout<<"\nContinuing to launch website 'xyz' now.";
// system("C:\\Program%Files\\Google\\Chrome\\Application\\chrome.exe");
ShellExecute, TEXT("open"), TEXT(c:\\program
files\\google\\chrome\\application\\chrome.exe), NULL, NULL, SW_SHOWNORMAL;
goto end;
end:
cout<<"\n\nProgram completed, Pess any key to exit. ";
_getch();
return 0;
}
Like this.
ShellExecute(NULL, TEXT("open"), TEXT("c:\\program files\\google\\chrome\\application\\chrome.exe"), NULL, NULL, SW_SHOWNORMAL);
You had missing parens, missing double quotes and quite possibly spurious characters between 'program' and 'files' in your path. You also had a missing parameter to ShellExecute.
Reading a book and learning a minimum of C++ syntax would be a good idea. These are very basic errors.
system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" http://heise.de");
works for me.
See How to call an external program with parameters?
How to run a C++ Console Program in full screen ? , using VS2008
Just tested this with cl fullscreen.cpp :
#include <iostream>
#include <windows.h>
#pragma comment(lib, "user32")
int main()
{
::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
std::cout << "Hello world from full screen app!" << std::endl;
std::cin.get();
}
Unfortunatelly it had duplicated the text on the second monitor :)
try:
#include <iostream>
using namespace std;
int main(){
system("mode 650");
system("pause");
return 0;
}
#include <windows.h>
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE),CONSOLE_FULLSCREEN_MODE,0);
There are not a lot of video adapters around these days that still support this. Run cmd.exe and press Alt+Enter. If you get a message box that says "This system does not support fullscreen mode" then you're done. If it does switch to full screen then you can use SetConsoleDisplayMode() in your main() function. Of course, you don't know what your customer's machine is like, best not to pursue this.
That's what I'm using:
system("mode con COLS=700");
ShowWindow(GetConsoleWindow(),SW_MAXIMIZE);
SendMessage(GetConsoleWindow(),WM_SYSKEYDOWN,VK_RETURN,0x20000000);
It removes the scrollbar :D
For full screen windowed mode: ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
Just a workaround: You could use some sort of earlier DOS video modi, for example...
asm
{
mov ax, 13h
push bp
int 10h
pop bp
}
...to have a resolution of 320x200 pixels.
But I'm not sure if this would work for a windows application... Probably not!
Just add this line (anywhere) before your output,
system("mode 650");
Such as,
#include<bits/stdc++.h>
using namespace std;
int main(){
system("mode 650");
cout<<"Hey, this words are shown in full screen console! "<<endl;
return 0;
}