-
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;
}
-
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