Click to See Complete Forum and Search --> : c: reading input without knowing size?
Fryguy8
11-18-2004, 04:53 PM
Is there any way to read from stdin (or from a file) without knowing the size before reading, and without wasting space by having too large of an array or malloc?
Doing an assignment for school, where we have to read a text file that just happens to be a cgi query, and then parse the query. When we read the file, we were told to go char by char through it to get a counter, and then allocate the char* and store it.
I thought the point of c was to be highly efficient? Reading a file 2x doesn't seem to be efficient to me...
bwkaz
11-18-2004, 07:28 PM
Using the fstat() system call would be a way to get the size in bytes of the file without having to read it. But fstat() is Unix-specific (it's not part of the C standard library). If that's OK, check out the fstat(2) manpage (man 2 fstat). There is also a chance that other processes could change the filesize between your fstat and your read, though, unless you've locked the file.
If that's not OK, then maybe it would be acceptable to read the file in chunks (say, 1024 byte chunks). Then you'd never allocate more than 1023 extra bytes, and you can always do an extra realloc at the end to trim the buffer.
Otherwise, you can use a single fixed-size buffer and the fgets() function...
Fryguy8
11-19-2004, 01:27 AM
so basically all of the variable length string stuff I'm used to in higher-level languages is a sham?
Well, maybe not so harsh, but it's safe to say that all of those are simply allocating x amount of memory, and then reallocating as it needs to, hidden from my view?
I guess I just need to get used to c being lower-level than the languages I'm more accustomed to using.
bwkaz
11-19-2004, 09:35 AM
Originally posted by Fryguy8
Well, maybe not so harsh, but it's safe to say that all of those are simply allocating x amount of memory, and then reallocating as it needs to, hidden from my view? That's exactly what they're doing, yes.
;)