I've written a simple program to connect to a Linux server using SSH via my C++ program. Here's the code:
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
string hostIP;
string username;
string password;
cout << "Welcome to SSH Program" << endl;
cout << "----------------------" << endl;
cout << "\nEnter host ip or name Example: \"capa.its.uow.edu.au\": ";
cin >> hostIP;
cout << "Enter username: ";
cin >> username;
cout << "\nConnecting...\n" << endl;
string composite = "ssh " + username + "#" + hostIP;
char command[100];
strcpy(command, composite.c_str());
system(command);
system("pause");
}
It runs well on Ubuntu, but when I compile this same code on Visual Studio in Windows and execute it, the console present me with this error: 'ssh.exe' is not recognized as an internal or external command, operable program or batch file.
This didn't make sense to me because OpenSSH Client is clearly installed on my computer and I'm able to establish an SSH connection if I directly enter ssh username#server.ip.address in the command prompt.
I thought it might be an issue with the environment paths and so in Visual Studio, I checked Project>Properties>VC++ Directories>Executable Directories. There I found C:\WINDOWS\System32\OpenSSH\ among other paths, located in the Evaluated value: box.
Doesn't this mean everything should run fine since the OpenSSH directory is located in the path? Also, like I said, I am able to connect via SSH if I enter the command directly into the command prompt instead of the program.
Please help. I've been really scratching my head over this since last night.
I've managed to solve this issue. It seems to have been a problem with the selected platform in Visual Studio. Initially the platform for the project was set for an x86 architecture. This is the reason why ssh.exe could not be accessed even though it was present in the environment path.
By changing the platform to 64-bit architecture in the project properties, the program could reference the correct environment variables and execute ssh.exe as intended.
Related
i am new to qt. Trying to run some simple application in QtCreator 3.2.1 which is based on QT5 on Debian system.
i create c++ plain project with cmake
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "please enter your name:\n";
cin >> name;
cout << "hello" << name << "\n";
return 0;
}
The problem is that when i launch the application, and input some string. The application console dont have any resp.
I confirm that the application can be run in my gnome-terminal.
After that, i check the same message from the stackoverflow.
there is a solution "set project run in terminal".
Then i look for that on the QtCreator 3.2.1. I cant find that menu. Only have the terminal setting.
unfortunately, whichever i choose,
1. /usr/bin/xterm -e
2. /usr/bin/gnome-terminal -x
3. /usr/bin/x-terminal-emulator -e
the problem still exist.
you must choose the "Run in Terminal" if you create CMake project with C++
I am pretty new to CGI's and wanted to give them a try. Here is my situation:
My website is hosted on a linux server, I have contacted the hosting company and they said as long as I use the cgi-bin folder there is no problem. So I wrote a very short c++ program that just output some text:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello from c++ cgi";
return 0;
}
Since its a linux server (and I am running Windows) I suppose I needed to compile the source in linux so a installed a virtual machine running Ubuntu. I then compiled the source (tested it in Ubuntu) and placed the binary file in the cgi-bin folder. Then I used a simple html anchor to link to it:
T
But when I click the link on my page I get the "500 Internal Server Error" message. I have also tried to rename the binary file to "cgiTest.cgi" and update the link but still the same problem.
Anybody have any idea where my problem lies?
You forgot to output any HTTP headers (the error log entry would give you a hint).
#include <iostream>
using namespace std;
int main()
{
cout << "Content-Type: text/plain" << endl << endl;
cout << "Hello from c++ cgi";
return 0;
}
Thanks for the help, turns out I had to change the permissions and the hosting server is 32bit while I compiled on 64bit Ubuntu. I now compiled on 32bit Ubuntu and voila!, its running. Thanks to all for the help in solving this issue!
Compiled through compiler like Code::Blocks, I have tried the following, bit it does not work:
/*Running Firefox.exe*/
#include <stdio.h>
/*using c++*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int x;
cout << "Checking if processor is available..." << endl;
/*System used here*/
if (system(NULL)) puts ("Proceed");
else exit (1);
cout<< "Executing Firefox..." << endl;
/*Having some error here saying not recognized as internal or external command*/
x = system ("C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
/*cout here*/
cout <<"The value returned was:" << x << endl;
return 0;
}
Is it because Firefox is not recognized as a system of windows? If so how can I run Firefox, or even Internet Explorer from code?
Run cmd.exe (Windows command shell) and enter the string C:/Program Files (x86)/Mozilla Firefox/firefox.exe at the command line, and you will see the same problem - i.e. the problem is with your command string rather than your C++ code.
The space in the path requires the command string to be quoted:
system ("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"") ;
or
system ("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"") ;
I believe you need to convert path to DOS format
Launch a command prompt cd into Mozilla Firefox folder
run this: for %I in (.) do echo %~sI
Copy the output to system command with \ replaced with \\ append firefox.exe at end
Is it because Firefox is not recognized as a system of windows?
If you open up cmd.exe and type in C:/Program Files... it won't work because spaces are used as a delimiter. Quote your path:
system("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"");
Although if you're targeting Windows you should consider using CreateProcess which saves you this trouble.
If so how can I run Firefox, or even Internet Explorer from code?
If you want to show a web page, use ShellExecute* and let the shell do the work. It will take care of launching Firefox, Internet Explorer, Chrome, or whatever browser the user has configured to view web pages.
*Read the Remarks section about initializing COM, first.
Try using windows API CreateProcess API
I want to use c++ to open a file on Mac OS.
If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program?
Here is the sample code:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
char * dir = getcwd(NULL, 0);
cout << "Current dir: " << dir << endl;
ifstream fin("hi.txt");
if (fin.is_open()) cout << "File is Open" << endl;
else cout << "File is not open" << endl;
fin.close();
return 0;
}
Use the value $(PROJECT_DIR) in the working directory in your scheme debug settings:
You can set a custom working directory for your project in Xcode. In Xcode 4 choose Edit Scheme from the Scheme menu in the project window toolbar to open the scheme editor. Select the Run step from the left side of the project editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the button on the right side of the text field to choose the working directory.
This is a really old post - updating some info for Xcode 12 (Sept 2020)
Step 1). Xcode -> Product -> Scheme -> Edit Scheme (or create a new one)
Step 2) RUN(DEBUG), Working Directory(Checkmark) and enter $(PROJECT_DIR) as a starting point.
You could use chdir(), see here: Change the current working directory in C++.
Or if you could always just issue a system call (stdlib.h): http://www.cplusplus.com/reference/clibrary/cstdlib/system/. This won't be portable, but it might be good enough for what you need.
For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio.
However, most of the programs we are writing make use of cin and cout for input/output. cout works fine as output, as you can see what it puts out in the application output, but there is no way to provide to cin as if it were on a console, like Visual Studio uses for its C++.
An example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter name: ";
cin >> name;
cout << "Your name is " << name << endl;
}
Is there a way to use a console or provide input to cin like in Visual Studio?
I am currently running OS X Leopard, if it matters.
In Preferences, under the Environment section, set the "Terminal" option to /Applications/Utilities/Terminal.app, as pointed out by Alex Martelli.
Then, in the Projects tab, under Run Settings, check the box marked "Run in Terminal".
Now, QtCreator will use Apple's built-in Terminal.app instead of Qt's console, allowing for interactive input.
If you're doing "console"-style apps with no GUI, Qt Creator may not be the most appropriate IDE -- why not try Apple's own XCode, which probably comes on your OS DVD (as a separate installer), and worst-case can be freely downloaded by registering at Apple Developer Connection?
Edit: as the OP indicates that all they need is the location to Mac's terminal app, that's easy: it's /Applications/Utilities/Terminal.app.
I thought I would just add that if you are using Windows 10 and Qt Creator close to v4.13.2.
And you are developing a general console project.
Go to menu:
Tools > Options > Build & Run > General
and Find Default for "Run in terminal": and select Enabled, as its disabled by default.
Now when you click Run it will automatically open a command prompt window. Provided your code compiles :)
The following solution is currently working.
go to:
Project settings > Run > mark "Run in terminal"
#include <QCoreApplication>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Your name is " << name << std::endl;
return a.exec();
}