UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » empty a char array[]

#1 2005-10-13 11:30 PM

ashucool83
Member
Registered: 2005-09-30
Posts: 48

Re: empty a char array[]

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

Offline

#2 2005-10-14 12:03 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: empty a char array[]

Offline

#3 2007-05-29 06:45 PM

Kapil
Member
Registered: 2007-05-28
Posts: 7

Re: empty a char array[]

Offline

#4 2007-05-30 05:28 AM

ayyaz
Guest

Re: empty a char array[]

memset(buffer,0x0,sizeof(buffer));
this is make buffer empty ...    very simple ....

#5 2007-05-30 12:32 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: empty a char array[]

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...

Offline

#6 2007-05-31 07:00 PM

Kapil
Member
Registered: 2007-05-28
Posts: 7

Re: empty a char array[]

Offline

#7 2007-05-31 08:06 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: empty a char array[]

char buffer[1024] = "";

Offline

  • Index
  • » C
  • » empty a char array[]

Board footer

Powered by FluxBB