Printing from an opened file character by character

This is a discussion on Printing from an opened file character by character within the C Programming forums, part of the General Programming Boards category; Hi, I'm having trouble with an assignment I'm doing. My code is split across 3 files. My header, main, and ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    21

    Printing from an opened file character by character

    Hi, I'm having trouble with an assignment I'm doing. My code is split across 3 files. My header, main, and functions, so I'll post all three.

    Header: cA6_proto.h
    Code:
    #define MaxLineSize 100
    #define TRUE 1
    #define FALSE 0
    
    
    // libraries
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //#include <malloc.h>
    #include <Windows.h>
    
    
    // prototypes
    int getSmallFileLength(const char *filename);
    void printWordsWrap(char *line[100], int maxLineLength);
    functions: functions.c
    Code:
    #include"cA6_proto.h"
    
    
    int getSmallFileLength(const char *filename)
    {
        // variable declarations:
        WIN32_FIND_DATA fileData = {0};
        HANDLE h = FindFirstFile(filename, &fileData);
        int fileLength = -1;
    
    
        if (h != INVALID_HANDLE_VALUE)
        {    
            fileLength = fileData.nFileSizeLow; // sets fileLenght to size of file
            FindClose(h); // closes files
        }
    
    
        return fileLength;
    }
    
    
    void printWordsWrap(char *line[100], int maxLineLength)
    {
        int i = 0;
        int j = 0;
        char *space = " ";
        int counter = -1;
        int startPosition = 0;
    
    
        for (i = 0; i < maxLineLength; i++)
        {
            if (strcmp(space, line[i]) != 0)
            {
                counter++;
            }
            if (strcmp(space, line[i]) == 0)
            {
                if (counter < maxLineLength)
                {
                    for (j = startPosition; j < counter; j++)
                    {
                        printf("%c", line[j]);
                    }
                    startPosition = counter;
                }
                else
                {
                    printf("\n");
                    for (j = startPosition; j < counter; j++)
                    {
                        printf("%c", line[j]);
                    }
                    startPosition = counter;
                    counter = 0;
                }
            }
        }
        return;
    }
    And my mainline:
    Code:
    #include"cA6_proto.h"
    
    
    int main(int argc, char *argv[])
    {
        // variable declarations
        FILE *fp = NULL;
        int fileLength = 0;
        int columnWidth = 40;
        char fileChars[100] = {0};
        int wordWrap = FALSE;
        int i = 0;
        char *s = "-s";
        char *w = "-w";
        char *space = " ";
        char *printWord[MaxLineSize] = {0};
        int stringCompare = 0;
        
        if (argc < 2)
        {
            printf("\n no file input on command line \n");
            return 2; // command line operations error
        }
        else
        {
            if ((stringCompare = strncmp(s, argv[1], 2)) == 0)
            {
                // [-s] exists on the command line, therefore the file will print with a 'word wrap' properties (not splitting up words at all)
                wordWrap = TRUE;
                if ((stringCompare = strncmp(w, argv[2], 2)) == 0) // scans second argument to check for presence of -w
                {
                    if (sscanf(argv[2], "-w%d", &columnWidth) != 1) // scans the user's '-w' statement to check for a number, if none is found sets it to the default of 40
                    {
                        columnWidth = 40; //error getting user width, set to 40 instead
                    }
                }
            }
            if ((stringCompare = strncmp(w, argv[1], 2)) == 0) // scans first argument for a -w, indicating a custom width
            {
                if (sscanf(argv[1], "-w%d", &columnWidth) != 1) // scans the user's '-w' statement to check for a number, if none is found sets it to the default of 40
                {
                    columnWidth = 40; //error getting user width, set to 40 instead
                }
            }    
            for (i=1; i < argc; i++)
            {
                const char *fileName = argv[i];
                if ((fp = fopen(fileName, "r")) == NULL) // open user given file to read from
                {
                    printf("\n error opening file to read from \n");
                    fclose(fp); // closes file and quits if there is an error opening it
                    return 1; // can't open file error
                }
                /*fileLength = getSmallFileLength(fileName);
                if (fileLength < 1)
                {
                    printf("\n error getting length of file \n");
                    fclose(fp); // close file
                    return 4; // fatal error: goes to end of program (exits)
                }
                if ((fileChars = (char *) malloc(fileLength)) == NULL)
                {
                    printf("\n error allocating memory \n"); // quits program if memory is not allocated correctly
                    fclose(fp); // closes file
                    free(fileChars); // frees memory
                    return 4; // fatal error: closes program
                } */
                while ((fgets(fileChars, MaxLineSize, fp)) != NULL) // MaxLineSize is a #define to 100
                {
                    printWordsWrap(fileChars, columnWidth);            
                }
                fclose(fp);
            }
        }
        return 0;
    }

    My issues are occurring at the print statement in line 70 of main. It doesn't like the fileChars parameter I give it and I'm not sure why.

    Also I have the get fileLength and malloc commented out, because I'm not sure I need it, do I need it?
    Last edited by SneakySnake; 12-07-2012 at 10:03 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    In proto.h and functions.c make the printWordsWrap function header look like this: void printWordsWrap(char *line, int maxLineLength)
    No you don't need getfilelength and malloc because you hard-coded the memory for filechars to be 100 bytes. As long as that's adequate.
    Last edited by nonoob; 12-07-2012 at 12:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing the size of a character
    By juice in forum C Programming
    Replies: 18
    Last Post: 09-25-2011, 01:35 AM
  2. Printing charcter by character
    By Ron in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 09:39 PM
  3. Replies: 7
    Last Post: 01-01-2008, 11:30 AM
  4. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 08:04 PM
  5. Printing a Character from Binary
    By SavesTheDay in forum C Programming
    Replies: 6
    Last Post: 02-19-2002, 01:35 PM

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