============================MODULEY======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ==========================ONLY C Codes HERE============================== //program & file names, myecho.exe //compiled using Visual C++ .Net #include //echo command-line argument //using array of character pointers int main(int argc, char *argv[]) { int i; for(i=1; i //another version of the myecho program, //command-line argument int main(int argc, char *argv[]) { while(--argc > 0) printf("%s%s",*++argv, (argc > 1) ? " ": ""); printf("\n"); return 0; } -------------------------------------------------------------------------------- //searchpattern.cpp, compiled using //Visual C++ .Net #include //maximum input line length #define MAXLINE 100 //Function prototypes... int getline(char line[], int max); int strindex(char source[], char searchfor[]); //pattern to be searched for in the line char pattern[] = "eat"; //find/display all line matching pattern int main() { char line[MAXLINE]; int found = 0; printf("Searching \'eat\' in the line of text\n"); printf("Enter line of text:\n"); while(getline(line, MAXLINE) > 0) if(strindex(line, pattern) >= 0) { printf("%s", line); found++; } return found; } //getline, get line into string str, return the length int getline(char str[], int lim) { int count, i; i=0; while(--lim > 0 && (count=getchar()) != EOF && count != '\n') str[i++] = count; if(count=='\n') str[i++] = count; str[i]='\0'; return i; } //strindex, return index of t in str, -1 if none int strindex(char str[], char t[]) { int i, j, k; for(i=0;str[i]!='\0';i++) { for(j=i, k=0; t[k] != '\0' && str[j] == t[k]; j++, k++) ; if(k > 0 && t[k] == '\0') return i; } return -1; } -------------------------------------------------------------------------------- //searchpattern.cpp, compiled using VC++ .Net //not usable...just program skeleton #include #include #define MAXLINE 100 int getline(char *line, int max) { printf("In getline() function\n"); //put getting line of text codes here… return 0;} //find and print lines that match pattern //from the 1st argument int main(int argc, char *argv[]) { char line[MAXLINE]; int found = 0; //If just program name (argc = 1), then... if(argc != 2) printf("Usage: searchpattern thepattern\n"); //If the program name with switch/option (argc = 2), then... else while(getline(line, MAXLINE) > 0) { if(strstr(line, argv[1]) != NULL) {printf("%s", line); found++;} } return found; } -------------------------------------------------------------------------------- //searchpattern.cpp, compiled using VC++ .Net #include #include #define MAXLINE 100 int getline(char *line, int max) { int count, i; i=0; while(--max > 0 && (count=getchar()) != EOF && count != '\n') line[i++] = count; if(count=='\n') line[i++] = count; line[i]='\0'; return i; } //find and print lines that match the //pattern from the 1st argument int main(int argc, char *argv[]) { char line[MAXLINE]; int found = 0; //If just program name (argc = 1), then... if(argc != 2) printf("Usage: searchpattern thepattern\n"); //If the program name with switch/option (argc = 2), then... else while(getline(line, MAXLINE) > 0) { if(strstr(line, argv[1]) != NULL) { printf("%s", line); found++; } } return found; } -------------------------------------------------------------------------------- //searchpattern.cpp, compiled using //VC++ .Net #include #include //maximum input line length #define MAXLINE 100 int getline(char line[], int max) { int count, i; i=0; while(--max > 0 && (count=getchar()) != EOF && count != '\n') line[i++] = count; if(count=='\n') line[i++] = count; line[i]='\0'; return i; } //find all line of text matching pattern //supplied by command line argument int main(int argc, char *argv[]) { char line[MAXLINE]; long linenum = 0; int theoption, except = 0, number = 0, found =0; //check the minus sign.... while(--argc > 0 && (*++argv)[0] == '-') //check the option... while(theoption = *++argv[0]) switch (theoption) { case 'v': except = 1; break; case 'n': number = 1; break; default: printf("searchpattern: illegal option %s\n",theoption); argc = 0; found = -1; break; } // if(argc != 1) { //some help... printf("Usage: searchpattern -v -n thepattern\n"); printf("Try: searchpattern -v -n test\n"); printf("Try: searchpattern -v test\n"); printf("Try: searchpattern -n test\n"); printf("Try: searchpattern -vn test\n"); printf("Then enter line of text, with or w/o the pattern!\n"); } // else while(getline(line, MAXLINE) > 0) { linenum++; if((strstr(line,*argv) != NULL) != except) { if(number) printf("%ld:", linenum); printf("%s", line); found++; } } return found; } --------------------------------------------------------------------------------------------------- //main()arguments program example //C++ codes #include //For Borland 5.02 you may use instead of //and comment out the ‘using namespace std’; #include using namespace std; int main(int argc, char *argv[], char *envp[]) { //The default is no line numbers. int LineNum = 0; //If /n is passed to the .exe program, display //numbered listing of environment variables. //If program name and switch/option... AND stricmp... if((argc == 2) && stricmp(argv[1], "/n" ) == 0) LineNum = 1; else cout<<"no \'/n\' passed..."< //For VC++ .Net, comment out the above #include //and uncomment the following… //include //using namespace std; int main(int argc, /*Number of strings in array argv*/ char *argv[], /*Array of command-line argument strings*/ char **envp) /*Array of environment variable strings*/ { int count; /*Display each command-line argument.*/ printf("\nThe command-line arguments:\n"); for(count = 0; count < argc; count++) printf(" argv[%d] %s\n", count, argv[count]); /*Display each environment variable.*/ printf("\nEnvironment variables:\n"); while(*envp != NULL) printf(" %s\n", *(envp++)); return 0; } --------------------------------------------GCC------------------------------------------------------ /***************cmdline.c****************/ /***********Run on FeDorA 3 Machine********/ #include /*For C++ compiler, comment out the above #include */ /*and uncomment the following.*/ /*include */ /*using namespace std;*/ int main(int argc, /*Number of strings in array argv*/ char *argv[], /*Array of command-line argument strings*/ char **envp) /*Array of environment variable strings*/ { int count; /*Display each command-line argument.*/ printf("\nThe command-line arguments:\n"); for(count = 0; count < argc; count++) printf(" argv[%d] %s\n", count, argv[count]); /*Display each environment variable.*/ printf("\nEnvironment variables:\n"); while(*envp != NULL) printf(" %s\n", *(envp++)); ======================================================================================