Main Menu

Home
MASM
C
C/MASM
Java
Google
 
Home arrow MASM arrow File Manipulation
File Manipulation
/* ==================================================================================================
    This program prints a file 50 characters per line, counts the number of characters in the file,
    individually tallies the number of lower-case letters in the file, tallies other characters in
    the file as a whole, calculates the percentage of occurance of the different characters, and
    displays them in order of frequency.
   ==================================================================================================
*/

    #include <stdio.h>
    #include <stdlib.h>
    #include <crtdbg.h>

/* Prototype Declarations */
int        getFileSize        (FILE *textFile);
void    fileToBuffer    (FILE *textFile,    char *inBuffer,    int size);
void    printFile        (char *inBuffer,    int size);
int        countChars        (int count[],        char chars[],    char *inBuffer, int size);
void    printFileStats    (int count[],        char chars[],    int countSize);

    int main (void)
{
/* Local Definitions */
    FILE    *textFile;
    char    *inBuffer;
    int        size;
    int        countSize;
    int        count[27] = {0};
    char    chars[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','*'};

/* Statements */
    if (!(textFile = fopen("words.txt","r")))
    {
        printf ("\aError opening answer file\n");
        exit(100);
    }

    size = getFileSize(textFile);
    rewind(textFile);

    if(!(inBuffer = malloc(size*sizeof(char))))
        printf("\aMemory overflow\n"),exit(100);

    fileToBuffer(textFile, inBuffer, size);

    if ((fclose(textFile)) == EOF)
    {
        printf("ERROR closing words.txt\n");
        exit(100);
    }

    printFile(inBuffer, size);

    countSize = countChars(count, chars, inBuffer, size);
    
    free(inBuffer);

    printFileStats(count, chars, countSize);

    printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");

    return 0;

}/* main */

/* =============================== getFileSize =========================
    Counts the number of characters in a file.
   =====================================================================
*/
int        getFileSize        (FILE *textFile)
{
/*  Local Definitions */
    int size = 0;

/*  Statements */
    while((fgetc(textFile)) != EOF)
        size++;

    return (size);
}/* getFileSize */

/* =============================== fileToBuffer =========================
    Copies the contents of a file to a buffer
   ======================================================================
*/
void fileToBuffer (FILE *textFile, char *inBuffer, int size)
{
/*  Local Definitions */
    char next;
    char *walker = inBuffer;

/*  Statements */
    *walker = fgetc(textFile);

    while((next = fgetc(textFile)) != EOF)
    {
        walker++;
        *walker = next;
    }

    return;
}/* fileToBuffer */

/* ================================= printFile ========================
    Prints the number of characters in a file and prints the file 50
    characters per line
   ====================================================================
*/
void printFile (char *inBuffer, int size)
{
/* Local Definitions */
    int i = 1;
    char *walker = inBuffer;

/* Statements */
    printf("1200 characters read\n\nContents of Input Buffer:\n\n");

    while(i <= size)
    {
        for( i; ( ( i % 51 ) != 0 ) && ( i <= size ); i++ )
        {
            printf("%c", *walker++);
        }

        size++;
        i++;

        printf("\n");
    }

    return;
}

/* =============================== countChars ===========================
    Individually tallies the lowercase letters in a buffer, tallies the
    other characters as a whole, and returns the total tally
   ======================================================================
*/
int countChars(int count[], char chars[], char *inBuffer, int size)
{
/* Local Definitions */
    int i = 0;
    int j = 0;
    int countSize = 0;
    char *walker = inBuffer;

/* Statemtents */
    for(i; i < size; i++)
    {
        j = 0;

        while(*walker != chars[j] && j < 27)
            j++;

        if(j < 27)
            count[j] += 1;
        else
            count[26] += 1;

        walker++;
        countSize++;
    }

    return countSize;

}/* countChars */

/* ======================================= printFileStats ===========================
    displays the tallies sorted from largest percentage to smallest.
   ==================================================================================
*/
void printFileStats    (int count[], char chars[],    int countSize)
{
/* Local Definitions */
    int i = 0;
    int j = 0;
    int largest = 0;

/* Statements */
    printf("\nRank\tChar\tCount\tPct\n");
    for(i; i < 27; i++)
    {
        for(j = 1; j < 27; j++)
        {
            if(count[j] > count[largest])
                largest = j;
        }

        printf("%d\t%c\t%d\t%4.2f%%\n", i + 1, chars[largest], count[largest], (((float)(count[largest]))/((float)countSize))*100);

        count[largest] = -1;
        largest = 0;
    }

    printf("-----------------------------------------\n");
    printf("\ttotal\t%d\t100.00%%\n\n", countSize);
    return;
}/* printFileStats */
 
Next >
© 2009 SourceRip
Joomla! is Free Software released under the GNU/GPL License.