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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -