C/C++ Programming Pearls

C/C++ Programming Pearls is a collection of C and C++ short, one page or less, programs for the active developer, student, or C/C++ connoisseur.   

The following is an example of a "Hello World" program that creates "HelloWorldFile" if it doesn't exist for reading at the beginning, or appending data at the end.  So, if "HelloWorldFile" does not exist, a new file is created. If the file does exist, data is not over written, instead, "Hello World" is appended to the end of the file.  Error checking is done in the open_myfile function; If the file can't be open, "EXIT_FAILURE" terminates the program. 

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

open_myfile(char *name)
    FILE *stream;

    errno = 0;
    stream = fopen(name, "a+");
    if (stream == NULL) {
	fprintf(stderr, "%s: Couldn't open file %s; %s\n", "fopen", name, strerror(errno));
    } else
	return stream;

main(int argc, char **argv)
    FILE *fp;

    fp = open_myfile("HelloWorldFile");
    fprintf(fp, "Hello World \n");

Making use of environment variables can be handy. wordexpExample1.c demonstrates the use of wordexp.  $USER prints out the actual user.  From the shell, if  X=13 is executed before running this program, then, 14 will be returned. Likewise, * causes the output to show everything in the current directory. And, '~' lists the home directory, for the account running the program.

#include <wordexp.h>

expand (const char *program)
  wordexp_t result;
  int i;

  /* Expand the string.   */
  switch (wordexp (program, &result, 0))
    case 0:/* Successful.  */
    case WRDE_NOSPACE:
      /* If the error was WRDE_NOSPACE,
         then perhaps only part of the result was allocated.  */
      wordfree (&result);
    default:                    /* Some other error.  */
      return -1;

     for(i=0; i < result.we_wordc; i++)
      wordfree (&result);


int main(int argc, char **argv)
 expand("'$USER' * ~   $(($x+1)) $USER ");

Need the time of day down to the msec (one millionth of a second)? gettimeofday.c is an example of this

/* C routine: sample gettimeofday with time in milliseconds
   mmc mchirico@users.sourceforge.net

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>

#include <stdio.h>

int main(int argc, char **argv)

  char buffer[30];
  struct timeval tv;

  time_t curtime;

  gettimeofday(&tv, NULL); 

  strftime(buffer,30,"%m-%d-%g  %T.",localtime(&curtime));



What's New
  • [08.06.03] About: time gettimeofday.c
  • [07.09.03] Memory:Function pointer sample   
  • [05.12.03] STL:map with a class   
  • [05.10.03] SQLite:Sample database API   
  • [05.08.03] Class:Start of class example  
  • [04.30.03] Memory:Start of tree example  
  • [04.28.03] C++:Read from command string or input stream  
  • [04.27.03] Compile: Creating your own configure and Makefile tutorial  
  • [04.26.03] STL: stringSLT.cc  
  • [04.24.03] Compile: prog1.c, test.c  
  • [04.23.03] Intro: helloWorld.c, wordexpExample1.c  
  • [04.23.03] DLL: dlopen.c, plugin.c  
SourceForge.net Logo