John Matthews wrote:But if you do want to count the number of lines first, a simple way is to just read a character at a time using fgetc() until you hit EOF, incrementing your line count if the character is the end-of-line '\n' character. Then, after allocating you array, use rewind() to go back to the beginning of the file and read the numbers.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Tim Holloway wrote:Basically what you'd do is the same thing that Java does with transparently-expandable resources such as AtrrayLists and StringBuffers. You pre-allocate a nominal pointer array for the incoming rows. If you use it all up. you allocate a new, larger array, copy the contents of the old array into it, and discard (free) the old array. So you need a master pointer that points to the row-pointer array, a count of used slots in that array, and the number of slots.
Mark Spen wrote:No i don't store dynamically the numbers in array.So i 'll try this solution with count the lines and when this end i'll call the rewind,right??
John Matthews wrote:
Tim Holloway wrote:Basically what you'd do is the same thing that Java does with transparently-expandable resources such as AtrrayLists and StringBuffers. You pre-allocate a nominal pointer array for the incoming rows. If you use it all up. you allocate a new, larger array, copy the contents of the old array into it, and discard (free) the old array. So you need a master pointer that points to the row-pointer array, a count of used slots in that array, and the number of slots.
Or you just keep it simple and use realloc() to extend the array as suggested, and not bother with arrays of pointers and copying/discarding.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Tim Holloway wrote:Actually, realloc() does itself do copying and discarding.
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
|