Need an example of Functions/parameter passing/and arrays. can anyone help?

This is a discussion on Need an example of Functions/parameter passing/and arrays. can anyone help? within the C Programming forums, part of the General Programming Boards category; Need an example of Functions/parameter passing/and arrays. can anyone help? Or know any good places to find code examples like ...

  1. #1
    Unregistered
    Guest

    Question Need an example of Functions/parameter passing/and arrays. can anyone help?

    Need an example of Functions/parameter passing/and arrays. can anyone help? Or know any good places to find code examples like these?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I feel truly benevolent tonight, here's full source code of one of my toy programs for you to examine.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LINECOUNT 15
    
    static void manPrint ( char *buf );
    static void addPage ( char *buf );
    static void listPages ( void );
    static void helpPage ( void );
    static void collectGarbage ( FILE *file );
    
    int main ( void )
    {
      char buffer[BUFSIZ];
      printf ( "WindowsXP Man Pages\n\n$: " );
      while ( fgets ( buffer, (int)sizeof buffer, stdin ) != NULL ) {
        /* Clear the trailing newline and test the input */
        buffer[strlen(buffer)-1] = '\0';
        if ( buffer[0] == 'm' || buffer[0] == 'M' )
          manPrint ( buffer );
        else if ( buffer[0] == 'a' || buffer[0] == 'A' )
          addPage ( buffer );
        else if ( buffer[0] == 'l' || buffer[0] == 'L' )
          listPages();
        else if ( buffer[0] == 'h' || buffer[0] == 'H' )
          helpPage();
        else if ( buffer[0] == 'e' || buffer[0] == 'E' )
          return EXIT_SUCCESS;
        else
          printf ( "Invalid input\n" );
        printf ( "\n$: " );
      }
      return EXIT_SUCCESS;
    }
    
    void manPrint ( char *buf )
    {
      FILE *man, 
           *page;
      int count = 0;
      char lineBuf[BUFSIZ],
           *temp;
      if ( ( man = fopen ( "man.txt", "r" ) ) != NULL ) {
        /* Eat the first user argument and get the next one */
        temp = strtok ( buf, " " );
        temp = strtok ( NULL, " " );
        if ( temp != NULL ) {
          /* Search for the item name in man. If found
          ** read the file path for that item and use it
          ** as the argument for opening page.
          */
          while ( fscanf ( man, "%s", lineBuf ) == 1 ) {
            if ( strcmp ( temp, lineBuf ) == 0 ) {
              (void)fscanf ( man, "%s", lineBuf );
              if ( ( page = fopen ( lineBuf, "r" ) ) != NULL ) {
                while ( fgets ( lineBuf, (int)sizeof lineBuf, page ) != NULL ) {
                  printf ( "%s", lineBuf );
                  count++;
                  if ( count == LINECOUNT ) {
                    /* Wait for user input after
                    ** a certain amount of lines.
                    ** Added for smaller resolution screens 
                    ** that can't hold a full page.
                    */
                    (void)getchar();
                    count = 0;
                  }
                }
                collectGarbage ( page );
              }
              else {
                /* Handle open failure for page */
                fprintf ( stderr, "ERROR %d: File open failure\n", errno );
                return;
              }
            }
          }
          collectGarbage ( man );
        }
        else {
          /* Handle NULL value temp */
          fprintf ( stderr, "ERROR %d: Invalid data\n", errno );
          return;
        }
      }
      else {
        /* Handle open failure for man */
        fprintf ( stderr, "ERROR %d: File open failure\n", errno );
        return;
      }
    }
    
    void addPage ( char *buf )
    {
      FILE *man;
      char *temp;
      if ( ( man = fopen ( "man.txt", "a" ) ) != NULL ) {
        fprintf ( man, "\n" );
        /* Eat the first argument and get the next one */
        temp = strtok ( buf, " " );
        while ( ( temp = strtok ( NULL, " " ) ) != NULL )
          /* Potential error, input is not checked for validity */
          (void)fprintf ( man, "%s ", temp );
          collectGarbage ( man );
        }
        else
          fprintf ( stderr, "ERROR %d: File open failure\n", errno );
    }
    
    void listPages ( void )
    {
      FILE *man;
      char tmpBuf[BUFSIZ];
      if ( ( man = fopen ( "man.txt", "r" ) ) != NULL ) {
        printf ( "Supported Page List\nType man <page> to view\n-----------------------\n" );
        while ( fscanf ( man, "%s", tmpBuf ) == 1 ) {
          printf ( "%s ", tmpBuf );
          fscanf ( man, "%s", tmpBuf );
        }
        printf ( "\n" );
        collectGarbage ( man );
      }
      else
        fprintf ( stderr, "ERROR %d: File open failure\n", errno );		
    }
    
    void helpPage ( void )
    {
      FILE *help;
      char buf[BUFSIZ];
      if ( ( help = fopen ( "help.txt", "r" ) ) != NULL ) {
        while ( fgets ( buf, (int)sizeof buf, help ) != NULL )
          printf ( "%s", buf );
    		printf ( "\n" );
        collectGarbage ( help );
      }
      else
        fprintf ( stderr, "ERROR %d: File open failure\n", errno );
    }
    
    void collectGarbage ( FILE *file )
    {
      if ( fclose ( file ) != 0 ) 
        fprintf ( stderr, "ERROR %d: File close failure\n", errno );
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just curious, why are your functions prototyped as 'static'? Additionally, your actual functions are not as prototyped, because you lack the keyword 'static' when you define them.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21