c - Exclude terminator when writing to file -


i'm receiving message through socket in c written buffer. message has terminator (@@) added end know when stop writing buffer, however, buffer larger message. there way write , not including terminator, throwing away rest of buffer?

maybe position pointer?

char *pos; file* fp = fopen("tempfile", "w+"); pos = strstr(buffer, "@@"); pos = '\0';    // maybe stop writing null?  fwrite(buffer, 1, buf_size /*too big*/, fp); fclose(fp); 

i need size of portion of buffer contains message, or maybe write file character. either way work.

statement fwrite(buffer, 1, buf_size, fp) write buf_size bytes, regardless of actual content of buffer , regardless if contains '\0' considered "string termination". way tell fwrite correct number of elements write; , can calculated through pointer arithmetics:

fwrite(buffer, 1, pos-buffer, fp) 

of course, you'll have check if pos != null , on; think straight forward.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -