Thread: Printing individual characters from fgets using an array

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

    Printing individual characters from fgets using an array

    Hi, I'm trying to write a program that takes a text file as an output, and prints the text of that file to a column size defined by the user.

    Here's my printing loop so far, when I run it, it just crashes the program. What should I change?

    Code:
    while ((fgets(fileChars, MaxLineSize, fp)) != NULL) // MaxLineSize is a #define to 100
                {
                    stringLen = strlen(fileChars);
                    printf("%d", stringLen);
                    if (stringLen > columnWidth)
                    {
                        for (j = 0; j < columnWidth; j++)
                        {
                            printf("%s", fileChars[j]);
                        }
                        printf("\n");
                        for (j = columnWidth; j < stringLen; j++)
                        {
                            printf("%s", fileChars[j]);
                        }
                    }
                    else
                    {
                        printf("%s",fileChars);
                    }
                    
                }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("%s", fileChars[j]);
    Use %c to print an individual char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining words in a file by reading individual characters.
    By HellaWhiteDude in forum C++ Programming
    Replies: 0
    Last Post: 10-14-2011, 02:10 PM
  2. Printing an array of inputted characters
    By Nephilim in forum C Programming
    Replies: 2
    Last Post: 03-13-2011, 08:45 AM
  3. How to access individual characters in an array?
    By LanguidLegend in forum C Programming
    Replies: 2
    Last Post: 02-08-2011, 06:29 AM
  4. Emulating Constants w/ Individual Characters
    By daltore in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 09:41 AM
  5. check individual characters on input
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 09:56 PM