Thread: help chack for array and fgets

  1. #1
    Unregistered
    Guest

    help chack for array and fgets

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<iostream.h>

    int main(void)
    {
    char msg[20]; /* 0 to 19 = 20 elements the last element should never be accessed */
    /* its reseved for the \0 so in this case 0 to 18 = 19 elements are */
    /* accessable */

    /* read a string from a stream ie stdin, stops reading when it reads n-1 chars */
    fgets(msg, 19, stdin); /* or a newline character whichever comes first */
    /* fgets retains the \n character which is also copied to the array which is */
    /* terminated by an \0 */

    cout<<msg;

    return 0;
    }

    /* The array msg has 20 elements 0 to 19 this 19th elements should never be accessed */
    /* because it stores the \0(NULL) now fgets reads n-1 chars ie 19-1=18 chars plus */
    /* the \n and the \0 therefor fgets has 18 input chars including the \n */

  2. #2
    Unregistered
    Guest
    sorry did'nt really put what I whanted are the above code and comments corect concerning the array with fgets.

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM