scan a file and print out a list of words in ASCII order : Structure Serialization « Structure « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C / ANSI-C » Structure » Structure SerializationScreenshots 
scan a file and print out a list of words in ASCII order

/*
Practical C Programming, Third Edition
By Steve Oualline
Third Edition August 1997 

ISBN: 1-56592-306-5
Publisher: O'Reilly
*/

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

struct node {
    struct node    *left;       /* tree to the left */
    struct node    *right;      /* tree to the right */
    char           *word;       /* word for this tree */
};

/* the top of the tree */
static struct node *root = NULL;

/*
 * memory_error -- write error and die                  *
 */
void memory_error(void)
{
    fprintf(stderr, "Error:Out of memory\n");
    exit(8);
}

/*
 * save_string -- save a string on the heap             *
 *                                                      *
 * Parameters                                           *
 *      string -- string to save                        *
 *                                                      *
 * Returns                                              *
 *      pointer to malloc-ed section of memory with     *
 *      the string copied into it.                      *
 */
char *save_string(char *string)
{
    char *new_string;   /* where we are going to put string */

    new_string = malloc((unsigned) (strlen(string1));

    if (new_string == NULL)
        memory_error();

    strcpy(new_string, string);
    return (new_string);
}
/*
 * enter -- enter a word into the tree                  *
 *                                                      *
 * Parameters                                           *
 *      node -- current node we are looking at          *
 *      word -- word to enter                           *
 */
void enter(struct node **node, char *word)
{
    int  result;        /* result of strcmp */

    char *save_string(char *);  /* save a string on the heap */

    /* 
     * If the current node is null, we have reached the bottom
     * of the tree and must create a new node.
     */
    if ((*node== NULL) {

  /* Allocate memory for a new node */
        (*node= malloc(sizeof(struct node));
        if ((*node== NULL)
            memory_error();

  /* Initialize the new node */
        (*node)->left = NULL;
        (*node)->right = NULL;
        (*node)->word = save_string(word);
  return;
    }
    /* Check to see where the word goes */
    result = strcmp((*node)->word, word);

    /* The current node already contains the word, no entry necessary */
    if (result == 0)
        return;

    /* The word must be entered in the left or right sub-tree */
    if (result < 0)
        enter(&(*node)->right, word);
    else
        enter(&(*node)->left, word);
}
/*
 * scan -- scan the file for words                      *
 *                                                      *
 * Parameters                                           *
 *      name -- name of the file to scan                *
 */
void scan(char *name)
{
    char word[100];     /* word we are working on */
    int  index;         /* index into the word */
    int  ch;            /* current character */
    FILE *in_file;      /* input file */

    in_file = fopen(name, "r");
    if (in_file == NULL) {
        fprintf(stderr, "Error:Unable to open %s\n", name);
        exit(8);
    }
    while (1) {
        /* scan past the whitespace */
        while (1) {
            ch = fgetc(in_file);

            if (isalpha(ch|| (ch == EOF))
                break;
        }

        if (ch == EOF)
            break;

        word[0= ch;
        for (index = 1; index < sizeof(word); ++index) {
            ch = fgetc(in_file);
            if (!isalpha(ch))
                break;
            word[index= ch;
        }
        /* put a null on the end */
        word[index'\0';

        enter(&root, word);
    }
    fclose(in_file);
}
/*
 * print_tree -- print out the words in a tree          *
 *                                                      *
 * Parameters                                           *
 *      top -- the root of the tree to print            *
 */
void print_tree(struct node *top)
{
    if (top == NULL)
        return;                 /* short tree */

    print_tree(top->left);
    printf("%s\n", top->word);
    print_tree(top->right);
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Error:Wrong number of parameters\n");
        fprintf(stderr, "      on the command line\n");
        fprintf(stderr, "Usage is:\n");
        fprintf(stderr, "    words 'file'\n");
        exit(8);
    }
    scan(argv[1]);
    print_tree(root);
    return (0);
}
  

           
       
Related examples in the same category
1. Write structure into file
w___w__w.___j_a_v_a2___s_.___c___o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.