Thread: Fgets to read dynamicallly 4m keyboard????

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Fgets to read dynamicallly 4m keyboard????

    How to read dynamically( any no of chars) from Keyboard using fgets...

    I am new to fgets.. Help me please......

    I tried ... where i have to make change...


    Code:
    int main()
    {
        char *str,*str1;int i=1;
        printf("Enter the string");
           str=(char *) malloc(sizeof(char)*40);
           str1=str;
           fgets(str,40,stdin);
           printf("%s",str1);
            while (str[strlen(str) - 1] != '\n')
             {   
                  ++i;
                  str=(char *)realloc(str,sizeof(char)*40*i);
                    fgets(str,40,stdin);
                    printf("%s",str1);
                    }
                    
                    printf("\n Now String contains %s",str1); // contains only last 40 chars..
                    
                    system("pause");
                    
                    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to read "any number" of characters, then you either have to start with a buffer big enough for "any number" (there is a limit to how much you can reasonably expect a human to type into a line, not to mention that MOST OS's have a limit to how long a line can be).

    Or you have to do something along the lines of what you are doing now, but reallocate the string as you go along, and concatenate together the parts.

    Or use getchar() or similar to read a character at a time, and expand the buffer (using realloc) as you run out of buffer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks matsp for ur Timely help....

    Now its Working ............

    Code:
    while...........
    {
     ++i;
                  str=(char *)realloc(str,sizeof(char)*40*i);
                    fgets(dummy,40,stdin);
                    strcat(str,dummy);
                   
                    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Mods) Flame wars and trolls
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 67
    Last Post: 09-10-2002, 05:37 PM
  2. read "control d" from the keyboard
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-01-2002, 08:39 PM
  3. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM