View Full Version : empty a char array[]
ashucool83
10-13-2005, 11:30 PM
hi
I have a char array
char buffer[1024];
read(buffer,"file.txt");//lets assume this function fills the buffer
Now, how do i empty the buffer ? I know I can always overrite it, but i still want to empty it cause my next read will not fill the entire buffer and so the values from the first read() still stick there.
Thanks
ashu
i3839
10-14-2005, 12:03 PM
Uhh, how do you mean? char buffer[1024] allocates 1024 bytes at program startup, or on the stack, depending where it is, so you can't get rid of it. If you want that then use char* buffer = malloc(1024) and then use free(buffer) to get rid of it.
But if you just want to clear the old data, then just do that with memset or so. If you don't want that, but something else, then just ignore the old data. read() returns the number of bytes read, so you know exactly how much of the data in buffer is new data, the rest you can simply ignore.
If you assume buffer is a valid string with proper NUL termination, then stop doing that, as it's unsafe to do.
Kapil
05-29-2007, 06:45 PM
Now, how do i empty the buffer ?
Do you mean, how to make buffer null? that is after every read, you want to remove contents of buffer?
ayyaz
05-30-2007, 05:28 AM
memset(buffer,0x0,sizeof(buffer));
this is make buffer empty ... very simple ....
RobSeace
05-30-2007, 12:32 PM
Yes, but I certainly wouldn't recommend doing that after every single read()...
memset() is usually very fast and efficient as can be, but it still requires a bit of
work, and in this case it just seems like utterly unnecessary work... If all you need
is to null-terminate your data, then just do that: take the return value from read(),
and stick a null character there in your buffer, after all of the read-in data (make sure
to have room for one more byte than read() can ever possibly return!)... But, setting
every single char to null, especially when you're going to overwrite a bunch of them
with another read() in a little while, seems horribly inefficient...
Kapil
05-31-2007, 07:00 PM
char buffer[1024];
To make the whole array null
buffer[1024] = {(char)NULL};
but I think the following will suffice
buffer[0] = (char)NULL;
RobSeace
05-31-2007, 08:06 PM
To make the whole array null
buffer[1024] = {(char)NULL};
No, that's not right... If you instead did:
char buffer[1024] = "";
That's what I think you're looking for... The {'\0'} approach will work there as well,
but only as an initializer, not as a normal assignment after variable declaration...
(The compiler probably wouldn't accept that as you had it, anyway... But, if it did,
it'd be very bad, because buffer[1024] is one byte off the end of the array...)
but I think the following will suffice
buffer[0] = (char)NULL;
Well, that'll suffice to turn buffer into an empty string, yes... But, if you've filled
part of buffer from a read() (as the original question implied), then you want to
instead do:
len = read (fd, buffer, sizeof (buffer) - 1);
if (len < 0) die ("failed to read!");
buffer[len] = '\0';
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.