DaedalusUsedPerl
DaedalusUsedPerl

Reputation: 772

Read any number of lines from standard input

For a class project, I have to read a file and store it's contents in an array to be sorted. The one nice thing is that the file will be piped in through standard input, but the only problem is that I can not assume that the file is not growing: I have to check for EOF. How do I do this using malloc() and realloc()?

Upvotes: 0

Views: 35

Answers (1)

David Grayson
David Grayson

Reputation: 87386

Here's one way: Use malloc to allocate an array of chars with a reasonable default size like 4096. Start reading characters from the standard input into the array until you get the EOF. Whenever the array fills up, call realloc to double the size of the array.

Upvotes: 1

Related Questions