C++ Read from a file using fstream

/**
  Read from a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]){

  // create the file handle, opening the file
  std::fstream myFile("text.txt", std::ios::in);

  // create a string to hold the line
  std::string line;

  if(myFile.is_open()){
    while(!myFile.eof()){
      getline(myFile, line);
      std::cout << line << std::endl;
    }
  } else {
    std::cout << "Could not open text.txt for reading." << std::endl;
    return -1;
  }

  return 0;
}
Posted in C++ | Tagged | Leave a comment

C++ Write to a file using fstream

/**
  Write to a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]){

  // create the file handle, opening the file as well
  std::fstream myFile("text.txt", std::ios::out);

  if(myFile.is_open()){
    myFile << "I am just some text in some file.";
    myFile.close();
    std::cout << "File written to." << std::endl;
  } else {
    // could not create/write to file
    std::cout << "Could not write to file." << std::endl;
    return -1;
  }

  return 0;
}
Posted in C++ | Leave a comment

C++ Palindrome Checker

/**
  Palindrome Checker
  Copyright 2009 Travis Crowder
  Published under the MIT License
*/

#include <iostream>
#include <algorithm>
#include <string>

bool isPalindrome(const std::string theWord);

int main(int argc, char* argv[]){

  std::string theWord;

  if(!argv[1]){
    std::cout << "Enter the word to check: ";
    std::cin >> theWord;
  } else {
    theWord = argv[1];
  }
  std::cout << std::endl;

  /**
    Make all of the characters lower case
  */
  int i = 0;
  while(theWord[i]){
    if(isupper(theWord[i]))
      theWord[i] = tolower(theWord[i]);
    i++;
  }

  std::cout << theWord << " is ";
  if(!isPalindrome(theWord))
    std::cout << "NOT ";
  std::cout << "a palindrome." << std::endl;
  return 0;
}

bool isPalindrome(const std::string theWord){
  std::string tmp = theWord;
  reverse(tmp.begin(), tmp.end());
  if(tmp == theWord)
    return true;
  return false;
}
Posted in C++ | Tagged | Leave a comment

PHP function to validate MySQL UUID or GUID

/**
   *  Function to validate a generated GUID / UUID
   *  @param string $guid GUID
   *  @return bool
   */
  function validGUID($guid){
    return preg_match('#^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$#', $guid);
  }
Posted in MySQL, PHP | Tagged , | Leave a comment

PHP function to emulate MySQL UUID

/**
   *  uuid
   *
   *  uuid() simply replicates MySQL's UUID function, which returns a 36
   *  character "random" hash (32 alphanumeric, 4 dashes).
   *  @return string
   */
  function uuid(){
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
      mt_rand(0, 0x0fff) | 0x4000,
      mt_rand(0, 0x3fff) | 0x8000,
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  }
Posted in MySQL, PHP | Tagged , | Leave a comment

Linux: Cleaning up your login tracks

So you’ve logged into a Linux machine but you don’t want anyone to know you were there. Why? Who cares. Here are a couple of things to clean up your tracks.

Once you log in, you’ll notice that message that tells you the last time and location you logged in from. To get rid of that, we need to clear out /var/log/wtmp and /var/log/lastlog. We can do that easily with cat > /var/log/wtmp and cat > /var/log/lastlog

To clear out the command history, execute history -c

Hope that helps!

Posted in Linux, Security | Leave a comment

C++ Guessing Game

/**
  Guessing Game
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>

int main(int argc, char* argv[]){

  /**
    Create our variable to hold the guess made
  */
  int guess = 0;

  /**
    Let's keep track of the number of guesses made.
  */
  int guesses = 0;

  /**
    Set the secret to be a random number between 1 and 100
    Remember to seed the random number generator
  */
  srand(time(0));
  int secret = rand() % 100 + 1;

  /**
    while our guess is not the secret, get a guess
  */
  while(guess != secret){
    std::cout << "Enter your guess, between 1 and 100: ";
    std::cin >> guess;
    guesses++;
    if(guess == secret){
      /** guess was correct */
      std::cout << "You've figured out the secret in " << guesses;
      if(guesses == 1)
        std::cout << " guess";
      else
        std::cout << " guesses";
      std::cout << "!  It was " << secret << "!" << std::endl;
    } else if(guess > secret){
      /** guess was too high */
      std::cout << "Your guess was too high!  Try again!" << std::endl;
    } else {
      /** our guess can only be lower, logically */
      std::cout << "Your guess was too low!  Try again!" << std::endl;
    }
  }

  return 0;
}
Posted in C++ | Leave a comment

PHP function to emulate Excel NormSInv

<?php

function NormSInv($p){
   $a1 = -39.6968302866538; $a2 = 220.946098424521; $a3 = -275.928510446969;
   $a4 = 138.357751867269; $a5 = -30.6647980661472; $a6 = 2.50662827745924;
   $b1 = -54.4760987982241; $b2 = 161.585836858041; $b3 = -155.698979859887;
   $b4 = 66.8013118877197; $b5 = -13.2806815528857; $c1 = -7.78489400243029E-03;
   $c2 = -0.322396458041136; $c3 = -2.40075827716184, $c4 = -2.54973253934373;
   $c5 = 4.37466414146497; $c6 = 2.93816398269878; $d1 = 7.78469570904146E-03
   $d2 = 0.32246712907004; $d3 = 2.445134137143; $d4 = 3.75440866190742;
   $p_low = 0.02425; $p_high = 1 - $p_low;
   $q = 0.0; $r = 0.0;
   if($p < 0 || $p > 1){
      throw new Exception("NormSInv: Argument out of range.");
   } else if($p < $p_low){
      $q = pow(-2 * log($p), 2);
      $NormSInv = ((((($c1 * $q + $c2) * $q + $c3) * $q + $c4) * $q + $c5) * $q + $c6) /
         (((($d1 * $q + $d2) * $q + $d3) * $q + $d4) * $q + 1);
    } else if($p <= $p_high){
      $q = $p - 0.5; $r = $q * $q;
      $NormSInv = ((((($a1 * $r + $a2) * $r + $a3) * $r + $a4) * $r + $a5) * $r + $a6) * $q /
         ((((($b1 * $r + $b2) * $r + $b3) * $r + $b4) * $r + $b5) * $r + 1);
    } else {
      $q = pow(-2 * log(1 - $p), 2);
      $NormSInv = -((((($c1 * $q + $c2) * $q + $c3) * $q + $c4) * $q + $c5) * $q + $c6) /
         (((($d1 * $q + $d2) * $q + $d3) * $q + $d4) * $q + 1);
    }
    return $NormSInv;
}
Posted in Excel, PHP | Tagged , , | Leave a comment