Thread: Validating the contents of a char buffer

  1. #1
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Unhappy Validating the contents of a char buffer

    Hello,
    I need to figure out how to validate the contents of a char buffer that contains a string of text. Any help and examples would be appreciated.

    Each line of input will have the following format:
    N1,N2,N3,N4,TEXT

    Where:
    N1 is either zero or one and indicates if the text should be bold.
    N2 is either zero or one and indicates if the text should be italics.
    N3 is either zero or one and indicates if the text should be underlined.
    N4 is a value between 1 and 7 and represents the font size
    TEXT is the text to display on the screen.

    For example:
    1,0,1,3,Welcome to mysite!



    Specifically that characters 1,3,5, and 7
    of the buffer contain commas. Characters 0,2 and 4 should
    contain a value of zero or one. Character 6 must contain a
    value between 1 and 7

    I probably have to define something like:

    Char buff[BUFSIZ]

    The instructor suggested we use "sscanf" as I did in previous
    code:
    <SNIP>
    for ( i = 0 ; i < 80 ; i++ ) {

    /* User enters line of text and it is placed in buff char array */
    printf( "Please enter a line of text ('$' to exit): " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );

    fflush(stdin);
    if (buff[0] == '\n')
    puts ("You entered a blank line!");
    printf( "Please enter a line of text ('$' to exit): " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );


    /* This checks to see if the user wants to end program */

    if ( buff[0] == '$' )
    break;
    /* This allocates space using malloc and the length of text in buff + 1 */
    myStructArray[i].text = malloc( strlen(buff)+1 );
    strcpy( myStructArray[i].text, buff ); /*copy myStructArray to char buff*/

    /* Prompt user if they want text in bold */
    /* Results are place in myStructArray[i].bold */
    do
    {
    printf( "\nDo you want text to be bold?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].bold = response;
    <SNIP>



    Here is what I have to work with:


    #include <stdio.h>

    /* Defines to improve radability */
    #define TRUE 1
    #define FALSE 0
    #define MAX_WEB_PAGE_LINES 256
    #define END_OF_INPUT '$'

    /* Global Types */
    struct webPageTextLineStruct
    {
    char text[256]; /* Holds a line of text */
    short isBold; /* 1 if TRUE, 0 if FALSE */
    short isItalics; /* 1 if TRUE, 0 if FALSE */
    short isUnderlined; /* 1 if TRUE, 0 if FALSE */
    short textSize; /* Font Size (1-7) */
    };
    typedef struct webPageTextLineStruct webPageTextLine;

    /* Function prototypes */
    void gatherWebPageText( webPageTextLine* lineArray );
    void createWeb(webPageTextLine* lineArray);

    int main()
    {



    int i;
    webPageTextLine lineArray[MAX_WEB_PAGE_LINES];


    /* Initialize Line Array */
    for (i=0; i < MAX_WEB_PAGE_LINES; i++)
    {
    lineArray[i].text[0] = '\0'; /* Null line */
    lineArray[i].isBold = 0; /* FALSE */
    lineArray[i].isItalics = 0; /* FALSE */
    lineArray[i].isUnderlined = 0; /* FALSE */
    lineArray[i].textSize = 1; /* Minimum text size */
    }

    /* Build an array of text to place on the web page. */
    gatherWebPageText( lineArray );

    /* Generate the HTML page */
    createWeb(lineArray);

    /* Notify the user that the HTML page generation is complete */
    puts(""); /* Blank line */
    puts(""); /* Blank line */
    puts("Congratulations!");
    puts("The generation of your HTML page is complete!");
    puts("The results can be found in the file 'phaseThree.html'");

    return 0; /* Successful completion. */
    }


    int getNumberInRange(int low, int high)
    {

    int value;

    /* Flip low and high if necessary. Just in case the caller was confused. */
    if (low > high)
    {
    /* Swap low and high */
    value = low;
    low = high;
    high = value;
    }

    /* Prompt the user to enter a number. Keep prompting until
    a number within the given range is entered. */
    do
    {
    fflush(stdin); /* Needed before a scanf() */
    scanf("%d",&value); /* Wait and read an integer from the user */

    /* If the value was out of range notify the user */
    if ((value < low) || (value > high))
    {
    printf("Invalid entry. Please try again: ");
    }

    /* Loop again if necessary */
    } while ((value < low) || (value > high));

    return value; /* Return the user-entered value */
    }

    void gatherWebPageText( webPageTextLine* lineArray )
    {

    char* ptr;
    short currentLine;
    currentLine = -1; /* Negative one because the do-while loop will increment to zero */

    do
    {
    currentLine++;

    /* Get the text for the line */
    puts(""); /* Blank line */
    puts("------------------------------------------------------------------------");
    puts("Please type the next line of text for the web page and then press enter.");
    puts(" If you would like a blank line simply press enter.");
    printf(" To finish entering lines press the '%c' character and press enter.\n",END_OF_INPUT);
    fflush(stdin); /* Needed before a gets() */
    ptr = gets(lineArray[currentLine].text);

    if ((ptr[0] != END_OF_INPUT) && /* Terminating character */
    (ptr[0] != '\0' )) /* blank line */
    {
    puts(""); /* Blank line */
    puts(""); /* Blank line */

    /* Does the user want the entered line to be bold? */
    printf("Do you want to show this line in bold? (Enter 0 for 'NO' or 1 for 'YES'): ");
    lineArray[currentLine].isBold = getNumberInRange(0,1);

    /* Does the user want the entered line to be italicized? */
    printf("Do you want to show this line in italics? (Enter 0 for 'NO' or 1 for 'YES'): ");
    lineArray[currentLine].isItalics = getNumberInRange(0,1);

    /* Does the user want the entered line to be underlined? */
    printf("Do you want this line to be underlined? (Enter 0 for 'NO' or 1 for 'YES'): ");
    lineArray[currentLine].isUnderlined = getNumberInRange(0,1);

    /* How large a font does the user want to display the text? */
    printf("How large do you want the text? 1 (smallest) to 7 (largest): ");
    lineArray[currentLine].textSize = getNumberInRange(1,7);
    }

    } while ((lineArray[currentLine].text[0] != END_OF_INPUT) &&
    ((currentLine+1) < MAX_WEB_PAGE_LINES));

    return;
    }


    void createWeb( webPageTextLine* lineArray)
    {

    /* Create the output file */
    FILE* outFile;
    short currentLine = 0;

    outFile = fopen("phaseTwo.html","w");

    /* All HTML files must start with this tag. */
    fprintf(outFile,"<html>\n");

    /* Write to the file HTML representing each line in the line array*/
    while (lineArray[currentLine].text[0] != END_OF_INPUT)
    {
    /* Opening BOLD tag */
    if (lineArray[currentLine].isBold == TRUE)
    {
    fprintf(outFile,"<B>");
    }
    /* Opening ITALICS tag */
    if (lineArray[currentLine].isItalics == TRUE)
    {
    fprintf(outFile,"<I>");
    }
    /* Opening UNDERLINE tag */
    if (lineArray[currentLine].isUnderlined == TRUE)
    {
    fprintf(outFile,"<U>");
    }
    /* Opening TEXT SIZE tag */
    fprintf(outFile,"<FONT SIZE=%d>",lineArray[currentLine].textSize);

    /* Line Text */
    if (lineArray[currentLine].text[0] == '\0') /* Handle blank lines */
    {
    fprintf(outFile,"<p>&nbsp;</p>");
    }
    else
    {
    fprintf(outFile,"<p>%s</p>",lineArray[currentLine].text);
    }

    /* Closing BOLD tag */
    if (lineArray[currentLine].isBold == TRUE)
    {
    fprintf(outFile,"</B>");
    }
    /* Closing ITALICS tag */
    if (lineArray[currentLine].isItalics == TRUE)
    {
    fprintf(outFile,"</I>");
    }
    /* Closing UNDERLINE tag */
    if (lineArray[currentLine].isUnderlined == TRUE)
    {
    fprintf(outFile,"</U>");
    }
    /* Closing TEXT SIZE tag */
    fprintf(outFile,"</FONT>\n");

    currentLine++;
    }

    fprintf(outFile,"</body>\n");

    /* All HTML files must end with this tag. */
    fprintf(outFile,"</html>\n");

    /* Close the output file */
    fclose(outFile);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf( buf, "%d,%d,%d,%d,%s", &la[x].isBold, &la[x].isItalic, &la[x].isUnderline, &la[x].fontSize, la[x].text );

    I believe that should do the trick. Then just double check your values once it scans. Additionally, you could check the return value of sscanf.

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

  3. #3
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks Quzah
    This should get me started

    Mattz

  4. #4
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Unhappy

    That sscanf example does not quite work, for some reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM