Thread: getting input problem

  1. #1
    Unregistered
    Guest

    getting input problem

    hi guys
    i hope you can help me...

    i am trying to get a list of inputs and put them dynamically (with malloc) in an array. but their sizes and counts are not prdicted.
    for ex: a asd dsa g hg .................. ferder ....
    i want it to stop when it sees "\n"..
    is there anyone to give a example code or a hint..

    thanks everyone..

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's various ways.... here's one

    - Use a buffer that is long enough to hold all input.
    - Once a string is in the buffer, determine it's length
    - malloc() memory to hold that many bytes (plus 1 for the null terminator)
    - string copy from the buffer to the new memory.

    And another:

    - malloc() a small amount of memory.
    - Load the string from input one char at a time.
    - if you run out of room in the malloc()'d memory, just realloc() asking for a few more bytes.
    - Continue loading one byte at a time....

    Have a go a writing something, then post your code (with code tags) when you have trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Do you mean that you read strings of different lengths and want to put them in an array where each array-element contains a string? Then you could use a linked list where each node contains a dynamic character array.

    Code:
    struct node
    {
        char *dyn_char_array;
        struct node *next_node;
    }

  4. #4
    Unregistered
    Guest

    silly of me

    i tried to write that program but got lots of warnings and errors

    int main(void)
    {
    int sum, i;
    ip = (void *) malloc(sizeof(input_array));
    for(i=0,sum=50 ; (c==getchar()) != '\n' ; i++)
    {
    if(i > sum) {
    ip=(void *) realloc(ip, sizeof(input_array+50));
    sum += 50;
    }
    input_array[i]=c;
    }
    sum=i;
    return 0;
    }

  5. #5
    Unregistered
    Guest

    shiro

    i am trying tom put them in an dynamically made array

  6. #6
    Unregistered
    Guest

    hammer

    you wrote:
    - malloc() a small amount of memory.
    - Load the string from input one char at a time.
    - if you run out of room in the malloc()'d memory, just realloc() asking for a few more bytes.
    - Continue loading one byte at a time....

    i am just trying to do so..

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - malloc() a small amount of memory.
    - Load the string from input one char at a time.
    - if you run out of room in the malloc()'d memory, just realloc() asking for a few more bytes.
    - Continue loading one byte at a time....
    Try this, I think it's fully correct:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT_BLOCK 50
    
    int main(void)
    {
    	int sum, c;
    	char *ip, *tmp;
    	
    	if ((ip = malloc(INPUT_BLOCK)) == NULL)
    	{
    		perror("malloc");
    		return (1);
    	}
    	
    	for (sum = 0;(c = getchar()) != '\n'; sum++)
    	{
    		if (sum == INPUT_BLOCK-2)
    		{
    			if ((tmp = realloc(ip, sum + INPUT_BLOCK)) == NULL)
    			{
    				perror ("realloc");
    				free (ip);
    				return (1);
    			}
    			ip = tmp;
    		}
    		ip[sum] = c;
    	}
    		
    	ip[sum] = '\0';
    	
    	printf ("Input string is >%s<\n", ip);
    	
    	free (ip);
    	
    	return(0);
    }
    Last edited by Hammer; 05-26-2002 at 12:25 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Hammer
    - Use a buffer that is long enough to hold all input.
    - Once a string is in the buffer, determine it's length
    - malloc() memory to hold that many bytes (plus 1 for the null terminator)
    - string copy from the buffer to the new memory.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LENGTH 50
    #define NUMBER_OF_STRINGS 5
    
    int main(void)
    {
    	char buffer[MAX_LENGTH];
    	char *ip[NUMBER_OF_STRINGS];
    	int len, i;
    	
    	for (i = 0; i < NUMBER_OF_STRINGS; i++)
    	{
    		printf ("Enter String number %d: ", i);
    		if (fgets(buffer, MAX_LENGTH, stdin) == NULL)
    			ip[i] = NULL;
    		else
    		{
    			len = strlen(buffer);
    			if (buffer[len-1] == '\n') buffer[len-1] = '\0', len--;
    			if ((ip[i] = malloc(len+1)) == NULL)
    			{
    				perror ("malloc");
    				return (1);
    			}
    			strcpy (ip[i], buffer);
    		}
    	}
    	
    	for (i = 0; i < NUMBER_OF_STRINGS; i++)
    	{
    		if (ip[i] != NULL)
    		{
    			printf ("String %d is: >%s<\n", i, ip[i]);
    			free(ip[i]);
    		}
    	}
    	
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Unregistered
    Guest

    thx

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 11:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM