Changing C++ Standard Output - c++

If I understand correctly, std::cout << "Test"; should output Test to the terminal emulator, but when I run a program using it, it instead creates a file called a.out. Is there a way to change this?

Try running the a.out file from inside a Terminal (if Linux/Mac) or cmd (if Windows) by running the following command:
./a.out
If it throws any permission exception in Linux/Mac then you should try running the following command first,
chmod +x ./a.out
and then run,
./a.out

Related

how do i run a c++ file in a separate terminal from another c++ file?

I am new to ubuntu and was exploring the terminal. I got stuck here. I have two c++ files x.cpp and y.cpp . I am running x from the first terminal. It has a line as follows:
system("gnome-terminal");
this opens a new terminal window. Next it goes like:
system("g++ y.cpp");
system("./a.out");
but this runs y in the same terminal window. I want y to run in the newly opened terminal window. Please help.
Each call to system() runs a separate new process, as a child of the calling process. There is no relationship between the processes (except that they have the same parent). Each call to system does not run another command in the same context as the previous call, like running on a shell command-line.
You can start gnome-terminal with a command to run (instead of a shell prompt) so you can use system() to start a gnome-terminal that runs the commands you want:
system("gnome-terminal -e 'sh -c \"g++ y.cpp && ./a.out\"'");
This will run the command gnome-terminal -e 'sh -c "g++ y.cpp && ./a.out"' (but you need to escape the double-quote characters to put the command inside a C++ string literal).
That tells gnome-terminal to run a shell (sh) with the command g++ y.cpp && ./a.out

how to make terminal prompt messages?

I have a image processing project in C++ using opencv. The program runs correctly and I get the desired output. However, I have some messages that I print out using the cout command. When I run the program using the terminal (./myprogram) the messages are displayed correctly. When I double click the executable file I get only the output (in my case a new video is created) But I do get the messages. How do I get the program to automatically prompt the messages when it is not run from the terminal.
PS: I use ubuntu 14.04
Create a script like this, lets call it run.sh:
#!/bin/sh
cd work_dir
./myProgram
read -r -p "Press any key..." key
Then do:
xterm -e run.sh
and make your desktop shortcut run this command instead of the program directly.

How can I get stdio output when running application as root?

I'm trying to run my application as root because I need to access low level hardware on my computer.
When I run the command:
./application_name
...it works, except gives an error that it needs root. However, when I run this:
sudo ./application_name
...I get no terminal ouput.
I've tested that every time that I run an executable on Linux as root, it doesn't print anything to terminal. How can I fix this?
Edit: somewhat of a test case provided (mobile so can't type out much):
sudo g++ test.cpp -o executable
sudo chmod +x executable
This works on Debian:
./executable
This doesn't:
sudo ./executable
test.cpp:
#include <iostream>
int main() {
std::cout << "Hello World!";
}
That behavior is really strange. Root permissions for that application should have no effect on std output.
For example, I made a simple test, a "hello world" that I ran as root on Debian OS and I had output in terminal.
A simple test to convince yourself that you should have the output, is to make a redirect to a file. For example sudo ./executable > output.txt and you'll see that everything should be OK.
Note that it should be strange if you don't have output from a simple "hello world".

How to run/compile a C++ program in Nitrous.io?

I am just starting to use Nitrous.IO and I can't seem to find any information on the web on how to run C++ programs you make in it. Any help on how to run a C++ app made in Nitrous would be a huge help.
All commands are to be run within the console
Step 1:
Get inside the appropriate directory using the following command:
cd ./folder/subfolder/etc.
Step 2:
Type in the following command to run a fileName.cpp program:
// This will make a separate file named 'fileName'
// within the directory you are working in.
// This can now be run in the console.
g++ -o fileName fileName.cpp
Step 3:
Type in the following command to run your program:
//The output will display in the console. Enjoy!
./fileName
ps: answer was there

Try to execute command line codes from c++ linux

I tried the following code, to communicate with the command line from c++ code.
#include<iostream>
#include<cv.h>
int main()
{
system("gnome-terminal");
system("cd");
}
The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory
, but it did not. am working in linux
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
If I try ls, it seems to be working fine!
My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
The system function creates a shell child process to execute the specified command.
cd is a shell command which changes the current working directory of that shell process only.
So the child's cd probably works fine, but it has no effect on your C++ program, which is a different process.
Instead, you probably want to look at the Linux system call chdir.
Thanks for your help!! This command worked perfectly fine from this link
https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
gnome-terminal -x sh -c 'command1; command2; exec bash'
and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.