I hope I explain this right.. but I need my C++ program to display an HTML website. The code that I have right now only displays the text. How do I make it so it is actually displayed like a website (using HTML code)? I am going to use the .exe file uploaded to a server to display the page.
#include <iostream>
using namespace std;
int main()
{
cout << "Content-type: text/plain\n\n";
cout << "<h2>My first CGI program</h2>\n";
cout << "<h1>This is a test</h1>\n";
cout << "<h3>Why is this not working</h3>\n";
return 0;
}
Your HTTP response needs the status line, and you should change the mime type to text/html:
cout << "HTTP/1.1 200 OK\n";
cout << "Content-Type: text/html\n\n";
cout << "<h2>My first CGI program</h2>\n";
cout << "<h1>This is a test</h1>\n";
cout << "<h3>Why is this not working</h3>\n";
Related
Below is my test.cpp file.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Content-type:text/html" << endl << endl;
cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "<title>Hello World - First CGI Program</title>" << endl;
cout << "</head>" << endl;
cout << "<body>" << endl;
cout << "<h2>Hello World! This is my first CGI program</h2>" << endl;
cout << "</body>" << endl;
cout << "</html>" << endl;
return 0;
}
Below is how I compiled the test.cpp file to test.cgi file.
g++ -g test.cpp -o test.cgi
Below is how I change mode of the test.cgi file.
chmod 755 test.cgi
There is no error at all.
But when I visit the page in the browser "localhost:8080/test.cgi", I get the following error:
C:/xampp/cgi-bin/test.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[cgi:error] [pid 22568:tid 1864] (9)Bad file descriptor: [client ::1:60380] AH01222: don't know how to spawn child process: C:/xampp/cgi-bin/test.cgi
The default cgi.cgi of Apache server works as well. It seems like the test.cgi file is invalid. Because I cannot even view the content of the file in Visual Studio Code, while I can view the content of the cgi.cgi file as well, the default file of Apache server.
When I run the following command line in the terminal, the content of the test.cgi file is printed successfully.
./test.cgi
How to create a valid cgi file from a cpp file?
I'm trying to get this simple C++ to give the browser output to display. After I submit a form, it's supposed to call this guy that's in my cgi-bin as a .cgi file.
#include <iostream>
using namespace std;
int main ()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
I set both my cgi-bin directory as well as my .cgi C++ program to permissions 755, but it still gives me a server error 500. I am pretty sure the path to my cgi-bin is correct too. Any ideas?
Did you compile the file and set this as your .cgi? Might be the problem if you are trying to just access the script without compiling first, (easy to make the mistake if you are used to working in PHP / other scripting languages)
Here is my code:
#include "MyClass.h"
#include <qstring.h>
#include <qdebug.h>
MyClass::MyClass()
{
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
int counter=0;
while(counter<ports.size())
{
QString portName = ports[counter].portName;
QString productId= ports[counter].productID;
QString physicalName = ports[counter].physName;
QString vendorId = ports[counter].vendorID;
QString friendName = ports[counter].friendName;
string convertedPortName = portName.toLocal8Bit().constData();
string convertedProductId = productId.toLocal8Bit().constData();
string convertedPhysicalName = physicalName.toLocal8Bit().constData();
string convertedVendorId = vendorId.toLocal8Bit().constData();
string convertedFriendName = friendName.toLocal8Bit().constData();
cout << "Port Name: " << convertedPortName << endl;
cout << "Product ID:" << convertedProductId << endl;
cout << "Physical Name: " << convertedPhysicalName << endl;
cout << "Vendor Id: " << convertedVendorId << endl;
cout << "Friend Name: " << convertedFriendName << endl;
cout << endl;
counter++;
}
}
I have connected "Dreamcheeky Thunder Missile Launcher" USB toy, but I am unable to get it's Vendor ID or product ID or atleast anything related to it! See the following image
But using USBDView software, I can get all the details. See the following image
What is matter with My code? Or if it is simply not suitable?
Just running the installer for the toy and checking what it comes up with, it doesn't describe any API or documentation for accessing it as a serial port.
If you used some sort of monitoring program on their program you could maybe reverse engineer how it commands the device.
It may be easier just to interface with their UI directly. Using a program like AHK or calling SendInput() to coordinates relative to the upper left corner of their UI, you could command the directions of the device.
EDIT: More links related to this:
Because the USB device doesn't get listed as a COM# (how serial port shows up), and it is a HID device, you need a library that can talk to that. Here are some links that should help you get there:
http://www.qtcentre.org/threads/41075-USB-HID-connect-on-QT
http://www.signal11.us/oss/hidapi/
https://github.com/iia/Qt_libusb
It also looks like the guys at Robo Realm have done it already:
http://www.roborealm.com/help/DC_Missile.php
http://www.roborealm.com/help/USB_HID.php
http://www.roborealm.com/tutorial/usb_missile_launcher/slide010.php
Hope that helps.
I need to create a c++ cgi app the accepts post data. I will be accepting a json object. How to I get the payload?
I can get the get data using the below
int main() {
bool DEBUG = true;
cout << "content-type: text/html" << endl << endl;
//WHAT GOES HERE FOR POST
json=?????
//THIS IS A GET
query_string = getenv("QUERY_STRING");
}
If the method type is POST (you may also want to check this) then the POST-data is written to stdin. You can therefore use standard methods like this:
// Do not skip whitespace, more configuration may also be needed.
cin >> noskipws;
// Copy all data from cin, using iterators.
istream_iterator<char> begin(cin);
istream_iterator<char> end;
string json(begin, end);
// Use the JSON data somehow.
cout << "JSON was " << json << endl;
This will read all data from cin into json until an EOF occurs.
Assuming apache:
The documentation is found here:
You will find it near the bottom but the post data is provided over stdin.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
bool DEBUG = true;
std::cout << "content-type: text/html\n\n"; // prefer \n\n to std::endl
// you probably don't want to flush immediately.
std::stringstream post;
post << std::cin.rdbuf();
std::cout << "Got: " << post.str() << "\n";
}
I am trying to run the following program:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double first=1.49, second=2.59, third=3.69, fourth=4.79;
inFile.open("prices.txt");
char response;
if(!inFile.fail())
{
cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
return -1;
}
}
outFile.open("prices.txt");
if (inFile.fail())
{
cout << "\n The file does not exist and can not be opened" << endl;
cout << "The file has been successfully opened for output." << endl;
outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
outFile.close();
exit(1);
cout << "The file has been successfully opened for output. " << endl;
outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
outFile.close();
return 0;
}
}
Yet this program will not write the values to the prices.txt file. If you run the program once it says the file does not exist. Running it a second time says the file is already there and if you want to overwrite it. The thing is searching my Mac I cannot find this file anywhere.
Any ideas what I am doing wrong with running it in Xcode? A friend runs the exact same code in Visual Studio 2008 and it works. Any help is appreciated.
You need to set the working directory for the executable since you are assuming that your data files are in the current working directory. In Xcode 3.x you set this in the first tab of Get Info for the executable. In Xcode 4.x it has been moved, but the principle is the same.
Alternatively you can change your data file paths (e.g. make them absolute) so that you do not make assumptions about the current working directory.
You may not have permission to write into the directory that you are trying to save the file too.
Also, there is an error in your program and I am sure if it is there for debugging reasons. You have
outFile.close();
exit(1);
But then shortly there after you try to write to the file, then close it again.