i have a problem with reading stdin of unknown size. In fact its a table in .txt file, which i get to stdin by calling parameter '<'table.txt. My code should look like this:
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
char words[10][1024];
int i=0;
while(feof(stdin)==0)
{
fgets(words[i],100,stdin);
printf("%s", words[i]);
i++;
}
return 0;
}
but there is the problem i dont know the nuber of lines, which in this case is 10(we know the number of characters in line - 1024). It would be great if someone know the solution. Thanks in advance.
I suggest that you use
malloc
andrealloc
to manage your memory. Keep track of how big your array is or how many entries it has, and callrealloc
to double its size whenever the array is not big enough.Op appears to need to store the data somewhere
Note:
ccount
includes the end-of-line character(s).You have hit on one of the issues that plagues all new C-programmers. How do I dynamically allocate all memory I need to free myself from static limits while still keeping track of my collection of 'stuff' in memory. This problem usually presents itself when you need to read an unknown number of 'things' from an input. The initial options are (1) declare some limit big enough to work (defeating the purpose), or (2) dynamically allocate a pointers as needed.
Obviously, the goal is (2). However, you then run into the problem of "How do I keep track of what I've allocated?" This in itself is an issue that dogs beginners. The problem being, If I dynamically allocate using a bunch of pointers, **How do I iterate over the list to get my 'stuff' back out? Also, you have to initialize some initial number of pointers (unless using an advanced data structure like a linked-list), so the next question is "what do I do when I run out?"
The usual solution is to allocate an initial set of pointers, then when the limit is reached, reallocate to twice as many as original, and keep going. (as Grayson indicated in his answer).
However, there is one more trick to iterate over the list to get your 'stuff' back out that is worth understanding. Yes, you can allocate with
malloc
and keep track of the number of pointers used, but you can free yourself from tying a counter to your list of pointers by initially allocating withcalloc
. That not only allocates space, but also sets the allocated pointers toNULL
(or 0). This allows you to iterate over your list with a simplewhile (pointer != NULL)
. This provides many benefits when it comes to passing your collection of pointers to functions, etc.. The downside (a minimal one) is that you get to write a reallocation scheme that usescalloc
to allocate new space when needed. (bummer, I get to get smarter -- but I have to work to do it...)You can evaluate whether to use
malloc/realloc
off-the-shelf, or whether to reallocate usingcalloc
and acustom reallocate function
depending on what your requirements are. Regardless, understanding both, just adds more tools to your programming toolbox.OK, enough jabber, where is the example in all this blather?
Both of the following examples simply read all lines from any text file and print the lines (with pointer index numbers) back to stdout. Both expect that you will provide the filename to read as the first argument on the command line. The only difference between the two is the second has the reallocation with
calloc
done is acustom reallocation function
. They both allocate255
pointers initially and double the number of pointers each time the limit is hit. (for fun, you can setMAXLINES
to something small like10
and force repeated reallocations to test).first example with reallocation in main()
second example with reallocation in custom function
Take a look at both examples. Know that there are many, many ways to do this. These examples just give one approach that provide example of using a few extra tricks than you will normally find. Give them a try. Drop a comment if you need more help.