Skip to content

Instantly share code, notes, and snippets.

@tochiz
Created August 3, 2011 12:34
Show Gist options
  • Save tochiz/1122530 to your computer and use it in GitHub Desktop.
Save tochiz/1122530 to your computer and use it in GitHub Desktop.
C++ fastcgi sample
#include <iostream>
#include <fstream>
#include <fcgi_stdio.h>
#include <unistd.h>
int main( int argc , char * argv[] )
{
int result;
unsigned long long cnt = 0;
const char* output = NULL;
while((result = getopt(argc, (char**)argv, "o:")) != -1){
switch(result){
case 'o':
if(optarg) output = optarg;
break;
}
}
if(output){
// reading counter.
std::ifstream fin( output, std::ios::in | std::ios::binary );
if (fin){
fin >> cnt;
}
fin.close();
}
while (FCGI_Accept () >= 0)
{
printf( "Content-Type: text/plain\n\n" );
printf( "%d\n", ++cnt);
if(output){
// writing counter.
std::ofstream fout;
fout.open( output, std::ios::out | std::ios::binary | std::ios::trunc);
if (fout) {
fout << cnt;
}
fout.close();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment