TextCounter C++

High Performance Compiled Hit Counter

TextCounter C++ is a compiled version of the popular TextCounter script. Written in C++, it provides the same text-based hit counting functionality but executes significantly faster with solid file locking for high-concurrency environments. Perfect for busy websites that need reliable visitor tracking.

Free Download
Version 2.0
C++ CGI

Overview

The C++ version of TextCounter offers dramatic performance improvements over the Perl version. As a compiled binary with optimized file locking, it handles concurrent requests efficiently and uses minimal server resources.

Performance Comparison
Metric Perl Version C++ Version
Execution Time ~20-50ms ~1-3ms
Memory Usage 2-5 MB ~150 KB
Requests/Second ~50 ~800
File Locking Basic Solid
Modification Ease Easy (edit script) Moderate (recompile)
Package Contents
File Description
textcounter.cpp C++ source code with file locking
Makefile Compilation instructions
README Compilation and installation guide
When to Use C++
  • High-traffic websites
  • Multiple counters needed
  • Server resource constraints
  • Concurrent access concerns
  • Performance is critical
When to Use Perl
  • Low to moderate traffic
  • Frequent customization
  • No compiler available
  • Quick deployment
  • Learning purposes

Features

Blazing Fast

Executes 15-25x faster than Perl version. Handles 800+ requests per second.

Solid File Locking

Advanced locking prevents count corruption under heavy concurrent access.

Minimal Footprint

Uses only 150KB of memory compared to 2-5MB for Perl.

Pure Text Output

Outputs plain text numbers that you can style with CSS however you want.

Low CPU Usage

Minimal CPU consumption per request. Native compiled code runs efficiently.

Reliable

Type-safe C++ code reduces runtime errors and improves stability.

Compilation & Installation

  1. Upload Source Files
    Upload textcounter.cpp and Makefile to your server.
  2. Configure Data File Path
    Edit textcounter.cpp and set the DATA_FILE constant to your desired path.
  3. Compile the Program
    Run: g++ -O2 -o textcounter textcounter.cpp or simply make
  4. Create Data File
    Create the counter data file: touch /var/www/data/counter.dat
    Set permissions: chmod 666 /var/www/data/counter.dat
  5. Move to CGI-BIN
    Move the compiled binary to your cgi-bin directory.
  6. Set Permissions
    Set executable permissions: chmod 755 textcounter
  7. Test the Program
    Test by accessing directly: http://yoursite.com/cgi-bin/textcounter
  8. Use in SSI
    Add to your .shtml pages: <!--#exec cgi="/cgi-bin/textcounter"-->
Starting Value Tip

To start your counter at a specific number: echo "10000" > /var/www/data/counter.dat

C++ Source Code Example

#include <iostream>
#include <fstream>
#include <sys/file.h>
#include <unistd.h>

using namespace std;

// Configuration - Edit this path
const char* DATA_FILE = "/var/www/data/counter.dat";

int main() {
    // Output HTTP header
    cout << "Content-type: text/plain\n\n";

    int count = 0;

    // Open file for reading and writing
    int fd = open(DATA_FILE, O_RDWR | O_CREAT, 0666);
    if (fd == -1) {
        cerr << "Error: Cannot open data file" << endl;
        return 1;
    }

    // Acquire exclusive lock
    if (flock(fd, LOCK_EX) == -1) {
        cerr << "Error: Cannot lock file" << endl;
        close(fd);
        return 1;
    }

    // Read current count
    ifstream infile(DATA_FILE);
    if (infile.is_open()) {
        infile >> count;
        infile.close();
    }

    // Increment count
    count++;

    // Write new count
    ofstream outfile(DATA_FILE, ios::trunc);
    if (outfile.is_open()) {
        outfile << count;
        outfile.close();
    }

    // Release lock
    flock(fd, LOCK_UN);
    close(fd);

    // Output count
    cout << count;

    return 0;
}
Makefile Example
CC = g++
CFLAGS = -O2 -Wall
TARGET = textcounter

all: $(TARGET)

$(TARGET): textcounter.cpp
	$(CC) $(CFLAGS) -o $(TARGET) textcounter.cpp

clean:
	rm -f $(TARGET)

install: $(TARGET)
	cp $(TARGET) /usr/local/apache/cgi-bin/
	chmod 755 /usr/local/apache/cgi-bin/$(TARGET)

.PHONY: all clean install

Usage Examples

Basic Counter Display
<!--#exec cgi="/cgi-bin/textcounter"-->
With Custom Styling
You are visitor number <span class="counter"><!--#exec cgi="/cgi-bin/textcounter"--></span>

<style>
.counter {
    font-family: 'Courier New', monospace;
    font-size: 2em;
    color: #00ff00;
    background: #000;
    padding: 5px 15px;
    border-radius: 5px;
    display: inline-block;
}
</style>
Retro 90s Style
<table border="1" bgcolor="#000000" cellpadding="5">
    <tr>
        <td>
            <font color="#00ff00" face="Courier New" size="+2">
                <b>Visitor #<!--#exec cgi="/cgi-bin/textcounter"--></b>
            </font>
        </td>
    </tr>
</table>

Download

Compressed Archives
  • textcounter-cpp.tar.gz 3.8 KB
  • textcounter-cpp.zip 4.2 KB
Individual Files
  • textcounter.cpp

    C++ source code with file locking

  • Makefile

    Compilation automation

  • README

    Compilation and installation guide

Frequently Asked Questions

The C++ version is compiled into native machine code with optimized file I/O and locking. There's no interpreter overhead, and the binary can handle 800+ requests per second compared to 50 for Perl. For high-traffic sites, this difference is dramatic.

The program uses flock() to acquire an exclusive lock on the data file before reading/writing. This ensures only one process can modify the count at a time, preventing corruption when multiple visitors access your site simultaneously. The lock is automatically released after updating.

Only if your hosting provider allows shell access and compilation. Most basic shared hosting doesn't provide these features. In that case, use the Perl version. VPS and dedicated servers fully support compilation.

Simply edit or delete the data file. To reset to zero: echo "0" > /var/www/data/counter.dat. To start at a specific number like 1000: echo "1000" > /var/www/data/counter.dat

Yes, but you'll need to modify the source code to read a query parameter and use different data files based on it. Alternatively, compile multiple versions with different data file paths and different binary names.