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?
Related
When I write a simple ofstream and ifstream instance ,ifstream is working but ofstream is not working. When I run this program ofstream is creating but I don't see output.txt in directory file.
I tried turning off COMODO Antivirus but the problem persists.
What is preventing me from seeing the output file?
here my First.cpp:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output("output.txt");
ifstream input("input.txt");
if (output.is_open()) {
cout << "ouput file is open" << endl;
output << "hi";
}
else {
cout << "output file is not open" << endl;
}
int number;
if (input.is_open()) {
cout << "input file is open" << endl;
input >> number;
cout << "Number:" << number << endl;
}
else {
cout << "input file is not open" << endl;
}
input.close();
output.close();
return 0;
}
here my input.txt:
21
12
22
23
32
when I run this program:
ouput file is open
input file is open
Number:21
I see that the issue was related to your Antivirus, but I'll write this anyways for others to read.
When dealing with IO its important to know what directory your executable will execute from. The working directory can be set in the project configuration. Right Click the project > Properties > Configuration Properties > Debugging > Working Directory. Configuration Window
The working directory is also the directory where all your application created files will be generated. For instance the example code generate output.txt at the project working directory.
std::ofstream output("output.txt");
if (!output.is_open())
return -1;
std::cout << "file is open" << std::endl;
output << "Hello there!";
output.flush();
output.close();
project directory screenshot
I am currently trying to read in a text file for use in a project but when running in release form (running the .exe file from a command window) the file doesn't read in. But yet in debug mode it works fine. I have tried having the files in the same directory as the .exe file but it doesn't seem to find it and I don't know why?
The code:
std::cout << "Acquiring data file..." << endl;
std::cout << "Reading data file..." << endl;
ifstream tempData;
tempData.open("temp_lincolnshire_short.txt");
if (tempData.is_open()) {
std::cout << tempData.rdbuf() << endl;
}
else {
cout << "Reading failed..." << endl;
}
tempData.close();
std::cout << "Data file closed." << endl;
What I ended up using was a relative path as identified on https://stackoverflow.com/a/35910234/3795116 which ended up being:
myFile.open("../Release/frequency.dat", ios::in);
*changing myFile to whatever your variable is.
I developed a code which create a .cpp file from a .isc file. This .isc file contains tons of lines with logic circuit information. My code read every line of this .isc file and write a code in a .cpp file that will simulate the logic of each .isc line, and this .cpp is saved in the same folder of the code which creates it. What I want to do is compile and run the executable of this .cpp file I created with a command line straight from my main code. I've been doing some researches and I found that a makefile could do that for me. About makefile I found some information here:
Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?
C++ makefile on Linux with Multiple *.cpp files
Based on that, After creating and writing the converted code in the .cpp file, I created a makefile (with dynamically name), here is it:
ofstream make_file("Makefile", ios::out | ios::trunc); //read and open the file
if (make_file == NULL ){ cout << "Error creating makefile!"; return 1; }
make_file << "# Makefile" << endl;
make_file << "# This makefile will run the new cpp file created\n" << endl;
make_file << "CC = g++\n" << endl;
make_file << "# FLAGS:" << endl;
make_file << "CFLAGS = -g -B -Wall\n" << endl;
make_file << "Executable target:" << endl;
make_file << "TARGET = " << netlist << "\n" << endl;
make_file << "all: $(TARGET)\n" << endl;
make_file << "$(TARGET): $(TARGET).c" << endl;
make_file << "\t$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).cpp\n" << endl;
make_file << "clean:" << endl;
make_file << "\t$(RM) $(TARGET)" << endl;
make_file.close();
So, my objective is to make this makefile compile the .cpp file and run its executable, assuming this is possible. If it is and I created the makefile in a correct way, how do I execute, or "make" it?
Edit: I'm using codeblocks
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)
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.