reading input problems

This is a discussion on reading input problems within the C Programming forums, part of the General Programming Boards category; Hi! good to be part of these forums I am trying to take multiline input from stdin, and then store ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    reading input problems

    Hi!
    good to be part of these forums
    I am trying to take multiline input from stdin, and then store it in a char ** array, where each char* array (ie string) is a line from the input. I am still not great at C programming and have searched alot but cant seem to find how to do this.

    -is it possible to create a char* array that can be dynamically sized to suit the length of the input line? (ie, so i dont waste space)

    -the code below does not work, i know its something silly in terms of logic, what am i doing wrong? or should i be going about this task a different way?

    Here is my attempt below:

    Code:
    #include<stdio.h>
    #define SIZE 10
    
    int main(int argc, char **argv)
    {
    
    	char c;
    	char **array=NULL;
    	int line=0;
    	int i=0;
    	int j=0;
        int size = 30;    /* number of characters that can be scanned in a line
                                        is there any way to allocate memory depending on how
                                        many characters are on each line? */
    
        /* allocate space for array of string arrays    */
    	array = malloc(sizeof(*array)*SIZE);
    
            /* while we arent scanning EOF or a newline then store it in the
                array of chars (ie. store that line as a string    */
    
    	while((c=getc(stdin)) != EOF)
    	{
    
    		/* if we are at the start of the line, allocate space for the
    		 * string being stored.
    		 */
    		if(i==0)
    		{
    		/* allocate space for the string being stored */
    		*array[line]=malloc(sizeof(char)*size);
    		}
    
    		/* if we are at the end of the line, then we prepare to store
    		 * the next line of input in the next string slot.
    		 */
    		if(c == "\n")
    			{
    				line++;
    				*(array+i) = "\0";	/* terminate string at end of line */
    				i=0;
    			}
    
            /* store the character in its correct position in the char array */
    		*(array+i) = c;
    
            /* increment i so we store next char in next position    */
    		i++;
    
    		}
    
    		/* now to check if it worked, just print all the strings out */
    		if(c==EOF)
    		{
    			for(j=0;j<line;j++)
    			{
    				printf("%s",*(array[j]));
    			}
    
    		}
    
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you described how it doesn't work - what is the difference between what it does and what you expect it to do?

    Here is one guess of what may be going wrong:
    Code:
    	if(i==0)
    	{
    	    /* allocate space for the string being stored */
    	    *array[line]=malloc(sizeof(char)*size);
    	}
    
    	/* if we are at the end of the line, then we prepare to store
    	 * the next line of input in the next string slot.
    	 */
    	if(c == "\n")
    	{
    	    line++;
    	    *(array+i) = "\0";	/* terminate string at end of line */
    	    i=0;
    	}
    
            /* store the character in its correct position in the char array */
    	*(array+i) = c;
    
    What happens when you have a newline on the line in red?

    As to how to allocate the right amount of memory for each line:
    1. I wouldn't worry about it in this case - just make it as long as it needs to be.
    2. You can do that in different ways - in a PC or such machine (that is, not in a small embedded system) I would make a ridiculously large buffer (e.g 1000 or 10000 characters) on the stack [local variable] that you read your line into, then when you find the newline, allocate enough memory to hold the string.
    In a case where using large local variables is unsuitable [embedded systems for example], I'd use either of these:
    a) start with a small buffer (16-32 bytes) allocated with malloc, then use realloc to grow it by a factor of 2x each time it is full.
    b) or allocate a large enough buffer [as above, 1000-10000 chars], and then use the method described when using a large local buffer. Free the temporary buffer at end of function.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 02:49 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. reading input files with different types of data
    By sanu in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21