Thread: Reading Input From stdin

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    Reading Input From stdin

    Recently I need a way to read a line from stdin, but it was quite important that I read the entire line (as some of the inputs were quite large). So I decided to have a go at writing a function, that reads, character by character input from stdin until it reaches either EOF or '\n'. It would then add the character that it read onto the end of a char array (malloc'ed) which is passed in as the first parameter. My code, however does not work (and even if it did it would have a good few problems), but this was the best that I could come up with:
    Code:
    int getLine (char *charPtr)
    {
    	char c;
    	int i;
    	int totalAlloc = 32;
    	charPtr = malloc(32);
    	for (i = 0; (c = fgetc(stdin)) != EOF || c != '\n'; i++)
    	{
    		if ((i + 1) > totalAlloc)
    		{
    			realloc(charPtr, totalAlloc + 32);
    			totalAlloc += 32;
    		}
    		charPtr[i] = c;
    	}
    	i += 2;
    	realloc(charPtr, i);
    	charPtr[i - 1] = '\0';
    	return i;
    }
    The function assumes that the memory passed to it has been freed first. The idea is to reach each char from stdin and add it onto the end of the array, if the array is out of space then allocate some more. I am not sure why it does not work (compiles fine...) but the only problem that I can see is with realloc. If the memory that it allocates has a different address then I am in trouble, as I (do not think) that I am able to change the memory that the pointer parameter points to (by that I mean assign charPtr to a different memory address).
    Can anyone help me?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fgets().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    (c = fgetc(stdin)) != EOF || c != '\n'
    [edit]
    That's always true! for it to be false, c would have to be EOF and '\n' at the same time! :O
    [/edit]
    Last edited by dwks; 11-24-2005 at 02:48 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int getLine (char *charPtr)
    {
    	char c;  /* EOF is an int value; see the FAQ */
    	int i;
    	int totalAlloc = 32;  /* why 32? */
    	charPtr = malloc(32);
    	for (i = 0; (c = fgetc(stdin)) != EOF || c != '\n'; i++)
    	{
    		if ((i + 1) > totalAlloc)
    		{
    			realloc(charPtr, totalAlloc + 32);  /* why 32? */
    			totalAlloc += 32;
    		}
    		charPtr[i] = c;
    	}
    	i += 2;  /* Why 2? */
    	realloc(charPtr, i);
    	charPtr[i - 1] = '\0';
    	return i;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    I did not use fgets as I do not know how much they will type. It could be up to (and if redirecting stdin) over 10k.
    I used the number 32 because, well, it seemed like a reasonable number to increment the size by, (I will #define it at some point when I get it all working).
    I add +=2 to i, because i has the number of chars they typed in -1 (arrays start at 0), now I need enough space for a '\0', so I need to allocate it to the size of i + 2 .
    I will try changing || (OR) to && (AND).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > realloc(charPtr, totalAlloc + 32);
    Try assigning the result somewhere.

    Better yet, check that realloc actually worked.
    Code:
    void *temp = realloc(charPtr, totalAlloc + 32);
    if ( temp != NULL ) {
      charPtr = temp;
    } else {
      /* failed - carry on doing something with charPtr */
      /* this should include free(charPtr) at some point */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  2. Reading and processing input from keyboard
    By papagaio in forum C Programming
    Replies: 1
    Last Post: 11-12-2008, 03:59 PM
  3. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM
  4. Reading input from a data file
    By prefect115 in forum C Programming
    Replies: 3
    Last Post: 09-26-2004, 12:30 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM